]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
Cleanup of the URL navigator, so that the DolphinMainWindow and the DolphinView are...
[dolphin.git] / src / bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
3 * *
4 * This program is free software; you can redistribute it and/or modify *
5 * it under the terms of the GNU General Public License as published by *
6 * the Free Software Foundation; either version 2 of the License, or *
7 * (at your option) any later version. *
8 * *
9 * This program is distributed in the hope that it will be useful, *
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12 * GNU General Public License for more details. *
13 * *
14 * You should have received a copy of the GNU General Public License *
15 * along with this program; if not, write to the *
16 * Free Software Foundation, Inc., *
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
18 ***************************************************************************/
19
20 #include "bookmarkselector.h"
21
22 #include <assert.h>
23 #include <q3popupmenu.h>
24 #include <qpainter.h>
25 #include <qpixmap.h>
26
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
30
31 #include "dolphinsettings.h"
32 #include "urlnavigator.h"
33
34 BookmarkSelector::BookmarkSelector(UrlNavigator* parent) :
35 UrlButton(parent),
36 m_selectedIndex(0),
37 m_urlNavigator(parent)
38 {
39 setFocusPolicy(Qt::NoFocus);
40
41 m_bookmarksMenu = new Q3PopupMenu(this);
42
43 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
44 KBookmark bookmark = root.first();
45 int i = 0;
46 while (!bookmark.isNull()) {
47 m_bookmarksMenu->insertItem(MainBarIcon(bookmark.icon()),
48 bookmark.text(),
49 i);
50 if (i == m_selectedIndex) {
51 QPixmap pixmap = SmallIcon(bookmark.icon());
52 setIcon(QIcon(pixmap));
53 setIconSize(pixmap.size());
54 setMinimumWidth(pixmap.width() + 2);
55 }
56 bookmark = root.next(bookmark);
57 ++i;
58 }
59
60 connect(m_bookmarksMenu, SIGNAL(activated(int)),
61 this, SLOT(slotBookmarkActivated(int)));
62
63 setMenu(m_bookmarksMenu);
64 }
65
66 BookmarkSelector::~BookmarkSelector()
67 {
68 }
69
70 void BookmarkSelector::updateSelection(const KUrl& url)
71 {
72 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
73 KBookmark bookmark = root.first();
74
75 int maxLength = 0;
76 m_selectedIndex = -1;
77
78 // Search the bookmark which is equal to the Url or at least is a parent Url.
79 // If there are more than one possible parent Url candidates, choose the bookmark
80 // which covers the bigger range of the Url.
81 int i = 0;
82 while (!bookmark.isNull()) {
83 const KUrl bookmarkUrl = bookmark.url();
84 if (bookmarkUrl.isParentOf(url)) {
85 const int length = bookmarkUrl.prettyUrl().length();
86 if (length > maxLength) {
87 m_selectedIndex = i;
88 setIcon(SmallIcon(bookmark.icon()));
89 maxLength = length;
90 }
91 }
92 bookmark = root.next(bookmark);
93 ++i;
94 }
95
96 if (m_selectedIndex < 0) {
97 // No bookmark has been found which matches to the given Url. Show
98 // a generic folder icon as pixmap for indication:
99 setIcon(SmallIcon("folder"));
100 }
101 }
102
103 KBookmark BookmarkSelector::selectedBookmark() const
104 {
105 return DolphinSettings::instance().bookmark(m_selectedIndex);
106 }
107
108 QSize BookmarkSelector::sizeHint() const
109 {
110 const int height = UrlButton::sizeHint().height();
111 return QSize(height, height);
112 }
113
114 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
115 {
116 QPainter painter(this);
117
118 const int buttonWidth = width();
119 const int buttonHeight = height();
120
121 QColor backgroundColor;
122 QColor foregroundColor;
123 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
124 isDisplayHintEnabled(DraggedHint);
125 if (isHighlighted) {
126 backgroundColor = KGlobalSettings::highlightColor();
127 foregroundColor = KGlobalSettings::highlightedTextColor();
128 }
129 else {
130 backgroundColor = palette().brush(QPalette::Background).color();
131 foregroundColor = KGlobalSettings::buttonTextColor();
132 }
133
134 // dimm the colors if the parent view does not have the focus
135 const bool isActive = m_urlNavigator->isActive();
136 if (!isActive) {
137 QColor dimmColor(palette().brush(QPalette::Background).color());
138 foregroundColor = mixColors(foregroundColor, dimmColor);
139 if (isHighlighted) {
140 backgroundColor = mixColors(backgroundColor, dimmColor);
141 }
142 }
143
144 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
145 // dimm the foreground color by mixing it with the background
146 foregroundColor = mixColors(foregroundColor, backgroundColor);
147 painter.setPen(foregroundColor);
148 }
149
150 // draw button backround
151 painter.setPen(Qt::NoPen);
152 painter.setBrush(backgroundColor);
153 painter.drawRect(0, 0, buttonWidth, buttonHeight);
154
155 // draw icon
156 const QPixmap pixmap = icon().pixmap();
157 const int x = (buttonWidth - pixmap.width()) / 2;
158 const int y = (buttonHeight - pixmap.height()) / 2;
159 painter.drawPixmap(x, y, pixmap);
160 }
161
162 void BookmarkSelector::slotBookmarkActivated(int index)
163 {
164 m_selectedIndex = index;
165
166 const KBookmark bookmark = selectedBookmark();
167 setPixmap(SmallIcon(bookmark.icon()));
168 emit bookmarkActivated(bookmark.url());
169 }
170
171 #include "bookmarkselector.moc"
172