]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
cleanup of unused forward declarations
[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 "dolphinsettings.h"
23 #include "urlnavigator.h"
24
25 #include <assert.h>
26
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
30 #include <kmenu.h>
31 #include <kdebug.h>
32
33 #include <QPainter>
34 #include <QPixmap>
35
36 BookmarkSelector::BookmarkSelector(UrlNavigator* parent) :
37 UrlButton(parent),
38 m_selectedIndex(0),
39 m_urlNavigator(parent)
40 {
41 setFocusPolicy(Qt::NoFocus);
42
43 m_bookmarksMenu = new KMenu(this);
44
45 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
46 KBookmark bookmark = root.first();
47 int i = 0;
48 while (!bookmark.isNull()) {
49 QAction* action = new QAction(MainBarIcon(bookmark.icon()),
50 bookmark.text(),
51 this);
52 action->setData(i);
53 m_bookmarksMenu->addAction(action);
54 if (i == m_selectedIndex) {
55 QPixmap pixmap = SmallIcon(bookmark.icon());
56 setIcon(QIcon(pixmap));
57 setIconSize(pixmap.size());
58 setMinimumWidth(pixmap.width() + 2);
59 }
60 bookmark = root.next(bookmark);
61 ++i;
62 }
63
64 connect(m_bookmarksMenu, SIGNAL(triggered(QAction*)),
65 this, SLOT(activateBookmark(QAction*)));
66
67 setMenu(m_bookmarksMenu);
68 }
69
70 BookmarkSelector::~BookmarkSelector()
71 {
72 }
73
74 void BookmarkSelector::updateSelection(const KUrl& url)
75 {
76 m_selectedIndex = baseBookmarkIndex(url);
77 if (m_selectedIndex >= 0) {
78 KBookmark bookmark = DolphinSettings::instance().bookmark(m_selectedIndex);
79 setIcon(SmallIcon(bookmark.icon()));
80 }
81 else {
82 // No bookmark has been found which matches to the given Url. Show
83 // a generic folder icon as pixmap for indication:
84 setIcon(SmallIcon("folder"));
85 }
86 }
87
88 KBookmark BookmarkSelector::selectedBookmark() const
89 {
90 return DolphinSettings::instance().bookmark(m_selectedIndex);
91 }
92
93 QSize BookmarkSelector::sizeHint() const
94 {
95 const int height = UrlButton::sizeHint().height();
96 return QSize(height, height);
97 }
98
99 KBookmark BookmarkSelector::baseBookmark(const KUrl& url)
100 {
101 const int index = baseBookmarkIndex(url);
102 return DolphinSettings::instance().bookmark(index);
103 }
104
105 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
106 {
107 QPainter painter(this);
108
109 const int buttonWidth = width();
110 const int buttonHeight = height();
111
112 QColor backgroundColor;
113 QColor foregroundColor;
114 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
115 isDisplayHintEnabled(DraggedHint);
116 if (isHighlighted) {
117 backgroundColor = KGlobalSettings::highlightColor();
118 foregroundColor = KGlobalSettings::highlightedTextColor();
119 }
120 else {
121 backgroundColor = palette().brush(QPalette::Background).color();
122 foregroundColor = KGlobalSettings::buttonTextColor();
123 }
124
125 // dimm the colors if the parent view does not have the focus
126 const bool isActive = m_urlNavigator->isActive();
127 if (!isActive) {
128 QColor dimmColor(palette().brush(QPalette::Background).color());
129 foregroundColor = mixColors(foregroundColor, dimmColor);
130 if (isHighlighted) {
131 backgroundColor = mixColors(backgroundColor, dimmColor);
132 }
133 }
134
135 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
136 // dimm the foreground color by mixing it with the background
137 foregroundColor = mixColors(foregroundColor, backgroundColor);
138 painter.setPen(foregroundColor);
139 }
140
141 // draw button backround
142 painter.setPen(Qt::NoPen);
143 painter.setBrush(backgroundColor);
144 painter.drawRect(0, 0, buttonWidth, buttonHeight);
145
146 // draw icon
147 const QPixmap pixmap = icon().pixmap();
148 const int x = (buttonWidth - pixmap.width()) / 2;
149 const int y = (buttonHeight - pixmap.height()) / 2;
150 painter.drawPixmap(x, y, pixmap);
151 }
152
153 void BookmarkSelector::activateBookmark(QAction* action)
154 {
155 assert(action != 0);
156 m_selectedIndex = action->data().toInt();
157
158 const KBookmark bookmark = selectedBookmark();
159 setPixmap(SmallIcon(bookmark.icon()));
160 emit bookmarkActivated(bookmark.url());
161 }
162
163 int BookmarkSelector::baseBookmarkIndex(const KUrl& url)
164 {
165 int index = -1; // return value
166
167 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
168 KBookmark bookmark = root.first();
169
170 int maxLength = 0;
171
172 // Search the bookmark which is equal to the Url or at least is a parent Url.
173 // If there are more than one possible parent Url candidates, choose the bookmark
174 // which covers the bigger range of the Url.
175 int i = 0;
176 while (!bookmark.isNull()) {
177 const KUrl bookmarkUrl = bookmark.url();
178 if (bookmarkUrl.isParentOf(url)) {
179 const int length = bookmarkUrl.prettyUrl().length();
180 if (length > maxLength) {
181 index = i;
182 maxLength = length;
183 }
184 }
185 bookmark = root.next(bookmark);
186 ++i;
187 }
188
189 return index;
190 }
191
192 #include "bookmarkselector.moc"
193