]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
Use a QLinkedList instead of Q3PtrList
[dolphin.git] / src / bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
3 * peter.penz@gmx.at *
4 * *
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. *
9 * *
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. *
14 * *
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 ***************************************************************************/
20
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 "bookmarkselector.h"
32 #include "dolphinsettings.h"
33 #include "dolphinview.h"
34 #include "dolphinmainwindow.h"
35 #include "urlnavigator.h"
36
37 BookmarkSelector::BookmarkSelector(UrlNavigator* parent) :
38 UrlButton(parent),
39 m_selectedIndex(0)
40 {
41 setFocusPolicy(Qt::NoFocus);
42
43 m_bookmarksMenu = new Q3PopupMenu(this);
44
45 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
46 KBookmark bookmark = root.first();
47 int i = 0;
48 while (!bookmark.isNull()) {
49 m_bookmarksMenu->insertItem(MainBarIcon(bookmark.icon()),
50 bookmark.text(),
51 i);
52 if (i == m_selectedIndex) {
53 QPixmap pixmap = SmallIcon(bookmark.icon());
54 setIcon(QIcon(pixmap));
55 setIconSize(pixmap.size());
56 setMinimumWidth(pixmap.width() + 2);
57 }
58 bookmark = root.next(bookmark);
59 ++i;
60 }
61
62 connect(m_bookmarksMenu, SIGNAL(activated(int)),
63 this, SLOT(slotBookmarkActivated(int)));
64
65 setMenu(m_bookmarksMenu);
66 }
67
68 BookmarkSelector::~BookmarkSelector()
69 {
70 }
71
72 void BookmarkSelector::updateSelection(const KUrl& url)
73 {
74 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
75 KBookmark bookmark = root.first();
76
77 int maxLength = 0;
78 m_selectedIndex = -1;
79
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.
83 int i = 0;
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) {
89 m_selectedIndex = i;
90 setIcon(SmallIcon(bookmark.icon()));
91 maxLength = length;
92 }
93 }
94 bookmark = root.next(bookmark);
95 ++i;
96 }
97
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"));
102 }
103 }
104
105 KBookmark BookmarkSelector::selectedBookmark() const
106 {
107 return DolphinSettings::instance().bookmark(m_selectedIndex);
108 }
109
110 QSize BookmarkSelector::sizeHint() const
111 {
112 const int height = UrlButton::sizeHint().height();
113 return QSize(height, height);
114 }
115
116 void BookmarkSelector::paintEvent(QPaintEvent* event)
117 {
118 QPainter painter(this);
119
120 const int buttonWidth = width();
121 const int buttonHeight = height();
122
123 QColor backgroundColor;
124 QColor foregroundColor;
125 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
126 isDisplayHintEnabled(DraggedHint);
127 if (isHighlighted) {
128 backgroundColor = KGlobalSettings::highlightColor();
129 foregroundColor = KGlobalSettings::highlightedTextColor();
130 }
131 else {
132 backgroundColor = palette().brush(QPalette::Background).color();
133 foregroundColor = KGlobalSettings::buttonTextColor();
134 }
135
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();
139
140 const bool isActive = (dolphin->activeView() == parentView);
141 if (!isActive) {
142 QColor dimmColor(palette().brush(QPalette::Background).color());
143 foregroundColor = mixColors(foregroundColor, dimmColor);
144 if (isHighlighted) {
145 backgroundColor = mixColors(backgroundColor, dimmColor);
146 }
147 }
148
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);
153 }
154
155 // draw button backround
156 painter.setPen(Qt::NoPen);
157 painter.setBrush(backgroundColor);
158 painter.drawRect(0, 0, buttonWidth, buttonHeight);
159
160 // draw icon
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);
165 }
166
167 void BookmarkSelector::slotBookmarkActivated(int index)
168 {
169 m_selectedIndex = index;
170
171 KBookmark bookmark = selectedBookmark();
172 setPixmap(SmallIcon(bookmark.icon()));
173
174 emit bookmarkActivated(index);
175 }
176
177 #include "bookmarkselector.moc"
178