]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
Changes to Undo/Redo in regard to ProgressIndicator
[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 "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 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 QSize BookmarkSelector::sizeHint() const
110 {
111 const int height = UrlButton::sizeHint().height();
112 return QSize(height, height);
113 }
114
115 void BookmarkSelector::paintEvent(QPaintEvent* event)
116 {
117 QPainter painter(this);
118
119 const int buttonWidth = width();
120 const int buttonHeight = height();
121
122 QColor backgroundColor;
123 QColor foregroundColor;
124 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
125 isDisplayHintEnabled(DraggedHint);
126 if (isHighlighted) {
127 backgroundColor = KGlobalSettings::highlightColor();
128 foregroundColor = KGlobalSettings::highlightedTextColor();
129 }
130 else {
131 backgroundColor = colorGroup().background();
132 foregroundColor = KGlobalSettings::buttonTextColor();
133 }
134
135 // dimm the colors if the parent view does not have the focus
136 const DolphinView* parentView = urlNavigator()->dolphinView();
137 const DolphinMainWindow* dolphin = parentView->mainWindow();
138
139 const bool isActive = (dolphin->activeView() == parentView);
140 if (!isActive) {
141 QColor dimmColor(colorGroup().background());
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::slotBookmarkActivated(int index)
167 {
168 m_selectedIndex = index;
169
170 KBookmark bookmark = selectedBookmark();
171 setPixmap(SmallIcon(bookmark.icon()));
172
173 emit bookmarkActivated(index);
174 }
175
176 #include "bookmarkselector.moc"
177