]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
I hope this compiles better
[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 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
77 KBookmark bookmark = root.first();
78
79 int maxLength = 0;
80 m_selectedIndex = -1;
81
82 // Search the bookmark which is equal to the Url or at least is a parent Url.
83 // If there are more than one possible parent Url candidates, choose the bookmark
84 // which covers the bigger range of the Url.
85 int i = 0;
86 while (!bookmark.isNull()) {
87 const KUrl bookmarkUrl = bookmark.url();
88 if (bookmarkUrl.isParentOf(url)) {
89 const int length = bookmarkUrl.prettyUrl().length();
90 if (length > maxLength) {
91 m_selectedIndex = i;
92 setIcon(SmallIcon(bookmark.icon()));
93 maxLength = length;
94 }
95 }
96 bookmark = root.next(bookmark);
97 ++i;
98 }
99
100 if (m_selectedIndex < 0) {
101 // No bookmark has been found which matches to the given Url. Show
102 // a generic folder icon as pixmap for indication:
103 setIcon(SmallIcon("folder"));
104 }
105 }
106
107 KBookmark BookmarkSelector::selectedBookmark() const
108 {
109 return DolphinSettings::instance().bookmark(m_selectedIndex);
110 }
111
112 QSize BookmarkSelector::sizeHint() const
113 {
114 const int height = UrlButton::sizeHint().height();
115 return QSize(height, height);
116 }
117
118 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
119 {
120 QPainter painter(this);
121
122 const int buttonWidth = width();
123 const int buttonHeight = height();
124
125 QColor backgroundColor;
126 QColor foregroundColor;
127 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
128 isDisplayHintEnabled(DraggedHint);
129 if (isHighlighted) {
130 backgroundColor = KGlobalSettings::highlightColor();
131 foregroundColor = KGlobalSettings::highlightedTextColor();
132 }
133 else {
134 backgroundColor = palette().brush(QPalette::Background).color();
135 foregroundColor = KGlobalSettings::buttonTextColor();
136 }
137
138 // dimm the colors if the parent view does not have the focus
139 const bool isActive = m_urlNavigator->isActive();
140 if (!isActive) {
141 QColor dimmColor(palette().brush(QPalette::Background).color());
142 foregroundColor = mixColors(foregroundColor, dimmColor);
143 if (isHighlighted) {
144 backgroundColor = mixColors(backgroundColor, dimmColor);
145 }
146 }
147
148 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
149 // dimm the foreground color by mixing it with the background
150 foregroundColor = mixColors(foregroundColor, backgroundColor);
151 painter.setPen(foregroundColor);
152 }
153
154 // draw button backround
155 painter.setPen(Qt::NoPen);
156 painter.setBrush(backgroundColor);
157 painter.drawRect(0, 0, buttonWidth, buttonHeight);
158
159 // draw icon
160 const QPixmap pixmap = icon().pixmap();
161 const int x = (buttonWidth - pixmap.width()) / 2;
162 const int y = (buttonHeight - pixmap.height()) / 2;
163 painter.drawPixmap(x, y, pixmap);
164 }
165
166 void BookmarkSelector::activateBookmark(QAction* action)
167 {
168 assert(action != 0);
169 m_selectedIndex = action->data().toInt();
170
171 const KBookmark bookmark = selectedBookmark();
172 setPixmap(SmallIcon(bookmark.icon()));
173 emit bookmarkActivated(bookmark.url());
174 }
175
176 #include "bookmarkselector.moc"
177