]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
commited initial version of Dolphin
[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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 "dolphin.h"
35 #include "urlnavigator.h"
36
37 BookmarkSelector::BookmarkSelector(URLNavigator* parent) :
38 URLButton(parent),
39 m_selectedIndex(0)
40 {
41 setFocusPolicy(QWidget::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 setPixmap(pixmap);
55 setMinimumWidth(pixmap.width() + 2);
56 }
57 bookmark = root.next(bookmark);
58 ++i;
59 }
60
61 connect(m_bookmarksMenu, SIGNAL(activated(int)),
62 this, SLOT(slotBookmarkActivated(int)));
63
64 setPopup(m_bookmarksMenu);
65 }
66
67 BookmarkSelector::~BookmarkSelector()
68 {
69 }
70
71 void BookmarkSelector::updateSelection(const KUrl& url)
72 {
73 KBookmarkGroup root = DolphinSettings::instance().bookmarkManager()->root();
74 KBookmark bookmark = root.first();
75
76 int maxLength = 0;
77 m_selectedIndex = -1;
78
79 // Search the bookmark which is equal to the URL or at least is a parent URL.
80 // If there are more than one possible parent URL candidates, choose the bookmark
81 // which covers the bigger range of the URL.
82 int i = 0;
83 while (!bookmark.isNull()) {
84 const KUrl bookmarkURL = bookmark.url();
85 if (bookmarkURL.isParentOf(url)) {
86 const int length = bookmarkURL.prettyURL().length();
87 if (length > maxLength) {
88 m_selectedIndex = i;
89 setPixmap(SmallIcon(bookmark.icon()));
90 maxLength = length;
91 }
92 }
93 bookmark = root.next(bookmark);
94 ++i;
95 }
96
97 if (m_selectedIndex < 0) {
98 // No bookmark has been found which matches to the given URL. Show
99 // a generic folder icon as pixmap for indication:
100 setPixmap(SmallIcon("folder"));
101 }
102 }
103
104 KBookmark BookmarkSelector::selectedBookmark() const
105 {
106 return DolphinSettings::instance().bookmark(m_selectedIndex);
107 }
108
109 void BookmarkSelector::drawButton(QPainter* painter)
110 {
111 const int buttonWidth = width();
112 const int buttonHeight = height();
113
114 QColor backgroundColor;
115 QColor foregroundColor;
116 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
117 isDisplayHintEnabled(DraggedHint);
118 if (isHighlighted) {
119 backgroundColor = KGlobalSettings::highlightColor();
120 foregroundColor = KGlobalSettings::highlightedTextColor();
121 }
122 else {
123 backgroundColor = colorGroup().background();
124 foregroundColor = KGlobalSettings::buttonTextColor();
125 }
126
127 // dimm the colors if the parent view does not have the focus
128 const DolphinView* parentView = urlNavigator()->dolphinView();
129 const Dolphin& dolphin = Dolphin::mainWin();
130
131 const bool isActive = (dolphin.activeView() == parentView);
132 if (!isActive) {
133 QColor dimmColor(colorGroup().background());
134 foregroundColor = mixColors(foregroundColor, dimmColor);
135 if (isHighlighted) {
136 backgroundColor = mixColors(backgroundColor, dimmColor);
137 }
138 }
139
140 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
141 // dimm the foreground color by mixing it with the background
142 foregroundColor = mixColors(foregroundColor, backgroundColor);
143 painter->setPen(foregroundColor);
144 }
145
146 // draw button backround
147 painter->setPen(NoPen);
148 painter->setBrush(backgroundColor);
149 painter->drawRect(0, 0, buttonWidth, buttonHeight);
150
151 // draw icon
152 const QPixmap* icon = pixmap();
153 if (icon != 0) {
154 const int x = (buttonWidth - icon->width()) / 2;
155 const int y = (buttonHeight - icon->height()) / 2;
156 painter->drawPixmap(x, y, *icon);
157 }
158 }
159
160 void BookmarkSelector::slotBookmarkActivated(int index)
161 {
162 m_selectedIndex = index;
163
164 KBookmark bookmark = selectedBookmark();
165 setPixmap(SmallIcon(bookmark.icon()));
166
167 emit bookmarkActivated(index);
168 }
169
170 #include "bookmarkselector.moc"
171