]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
19 ***************************************************************************/
23 #include <q3popupmenu.h>
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
31 #include "bookmarkselector.h"
32 #include "dolphinsettings.h"
33 #include "dolphinview.h"
34 #include "dolphinmainwindow.h"
35 #include "urlnavigator.h"
37 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
) :
41 setFocusPolicy(Qt::NoFocus
);
43 m_bookmarksMenu
= new Q3PopupMenu(this);
45 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
46 KBookmark bookmark
= root
.first();
48 while (!bookmark
.isNull()) {
49 m_bookmarksMenu
->insertItem(MainBarIcon(bookmark
.icon()),
52 if (i
== m_selectedIndex
) {
53 QPixmap pixmap
= SmallIcon(bookmark
.icon());
54 setIcon(QIcon(pixmap
));
55 setIconSize(pixmap
.size());
56 setMinimumWidth(pixmap
.width() + 2);
58 bookmark
= root
.next(bookmark
);
62 connect(m_bookmarksMenu
, SIGNAL(activated(int)),
63 this, SLOT(slotBookmarkActivated(int)));
65 setMenu(m_bookmarksMenu
);
68 BookmarkSelector::~BookmarkSelector()
72 void BookmarkSelector::updateSelection(const KUrl
& url
)
74 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
75 KBookmark bookmark
= root
.first();
80 // Search the bookmark which is equal to the Url or at least is a parent Url.
81 // If there are more than one possible parent Url candidates, choose the bookmark
82 // which covers the bigger range of the Url.
84 while (!bookmark
.isNull()) {
85 const KUrl bookmarkUrl
= bookmark
.url();
86 if (bookmarkUrl
.isParentOf(url
)) {
87 const int length
= bookmarkUrl
.prettyUrl().length();
88 if (length
> maxLength
) {
90 setIcon(SmallIcon(bookmark
.icon()));
94 bookmark
= root
.next(bookmark
);
98 if (m_selectedIndex
< 0) {
99 // No bookmark has been found which matches to the given Url. Show
100 // a generic folder icon as pixmap for indication:
101 setIcon(SmallIcon("folder"));
105 KBookmark
BookmarkSelector::selectedBookmark() const
107 return DolphinSettings::instance().bookmark(m_selectedIndex
);
110 QSize
BookmarkSelector::sizeHint() const
112 const int height
= UrlButton::sizeHint().height();
113 return QSize(height
, height
);
116 void BookmarkSelector::paintEvent(QPaintEvent
* event
)
118 QPainter
painter(this);
120 const int buttonWidth
= width();
121 const int buttonHeight
= height();
123 QColor backgroundColor
;
124 QColor foregroundColor
;
125 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
126 isDisplayHintEnabled(DraggedHint
);
128 backgroundColor
= KGlobalSettings::highlightColor();
129 foregroundColor
= KGlobalSettings::highlightedTextColor();
132 backgroundColor
= palette().brush(QPalette::Background
).color();
133 foregroundColor
= KGlobalSettings::buttonTextColor();
136 // dimm the colors if the parent view does not have the focus
137 const DolphinView
* parentView
= urlNavigator()->dolphinView();
138 const DolphinMainWindow
* dolphin
= parentView
->mainWindow();
140 const bool isActive
= (dolphin
->activeView() == parentView
);
142 QColor
dimmColor(palette().brush(QPalette::Background
).color());
143 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
145 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
149 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
150 // dimm the foreground color by mixing it with the background
151 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
152 painter
.setPen(foregroundColor
);
155 // draw button backround
156 painter
.setPen(Qt::NoPen
);
157 painter
.setBrush(backgroundColor
);
158 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
161 const QPixmap pixmap
= icon().pixmap();
162 const int x
= (buttonWidth
- pixmap
.width()) / 2;
163 const int y
= (buttonHeight
- pixmap
.height()) / 2;
164 painter
.drawPixmap(x
, y
, pixmap
);
167 void BookmarkSelector::slotBookmarkActivated(int index
)
169 m_selectedIndex
= index
;
171 KBookmark bookmark
= selectedBookmark();
172 setPixmap(SmallIcon(bookmark
.icon()));
174 emit
bookmarkActivated(index
);
177 #include "bookmarkselector.moc"