]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
reload view when the settings are applied
[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 "urlnavigator.h"
23
24 #include <assert.h>
25
26 #include <kiconloader.h>
27 #include <kglobalsettings.h>
28 #include <kbookmarkmanager.h>
29 #include <kmenu.h>
30 #include <kdebug.h>
31
32 #include <QPainter>
33 #include <QPixmap>
34 #include <kicon.h>
35
36 BookmarkSelector::BookmarkSelector(UrlNavigator* parent, KBookmarkManager* bookmarkManager) :
37 UrlButton(parent),
38 m_selectedAddress(),
39 m_urlNavigator(parent),
40 m_bookmarkManager(bookmarkManager)
41 {
42 setFocusPolicy(Qt::NoFocus);
43
44 m_bookmarksMenu = new KMenu(this);
45
46 KBookmarkGroup root = m_bookmarkManager->root();
47 KBookmark bookmark = root.first();
48 int i = 0;
49 while (!bookmark.isNull()) {
50 QAction* action = new QAction(MainBarIcon(bookmark.icon()),
51 bookmark.text(),
52 this);
53 m_bookmarksMenu->addAction(action);
54 QString address = QChar('/');
55 address += QString::number(i);
56 action->setData(address);
57 if (address == m_selectedAddress) {
58 QPixmap pixmap = SmallIcon(bookmark.icon());
59 setIcon(QIcon(pixmap));
60 setIconSize(pixmap.size());
61 setMinimumWidth(pixmap.width() + 2);
62 }
63 bookmark = root.next(bookmark);
64 ++i;
65 }
66
67 connect(m_bookmarksMenu, SIGNAL(triggered(QAction*)),
68 this, SLOT(activateBookmark(QAction*)));
69
70 setMenu(m_bookmarksMenu);
71 }
72
73 BookmarkSelector::~BookmarkSelector()
74 {
75 }
76
77 void BookmarkSelector::updateSelection(const KUrl& url)
78 {
79 KBookmark bookmark = m_bookmarkManager->root().closestBookmark(url);
80 if (!bookmark.isNull()) {
81 m_selectedAddress = bookmark.address();
82 setIcon(KIcon(bookmark.icon()));
83 }
84 else {
85 m_selectedAddress = QString();
86 // No bookmark has been found which matches to the given Url. Show
87 // a generic folder icon as pixmap for indication:
88 setIcon(KIcon("folder"));
89 }
90 }
91
92 KBookmark BookmarkSelector::selectedBookmark() const
93 {
94 return m_bookmarkManager->findByAddress(m_selectedAddress);
95 }
96
97 QSize BookmarkSelector::sizeHint() const
98 {
99 const int height = UrlButton::sizeHint().height();
100 return QSize(height, height);
101 }
102
103 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
104 {
105 QPainter painter(this);
106
107 const int buttonWidth = width();
108 const int buttonHeight = height();
109
110 QColor backgroundColor;
111 QColor foregroundColor;
112 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
113 isDisplayHintEnabled(DraggedHint);
114 if (isHighlighted) {
115 backgroundColor = KGlobalSettings::highlightColor();
116 foregroundColor = KGlobalSettings::highlightedTextColor();
117 }
118 else {
119 backgroundColor = palette().brush(QPalette::Background).color();
120 foregroundColor = KGlobalSettings::buttonTextColor();
121 }
122
123 // dimm the colors if the parent view does not have the focus
124 const bool isActive = m_urlNavigator->isActive();
125 if (!isActive) {
126 QColor dimmColor(palette().brush(QPalette::Background).color());
127 foregroundColor = mixColors(foregroundColor, dimmColor);
128 if (isHighlighted) {
129 backgroundColor = mixColors(backgroundColor, dimmColor);
130 }
131 }
132
133 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
134 // dimm the foreground color by mixing it with the background
135 foregroundColor = mixColors(foregroundColor, backgroundColor);
136 painter.setPen(foregroundColor);
137 }
138
139 // draw button backround
140 painter.setPen(Qt::NoPen);
141 painter.setBrush(backgroundColor);
142 painter.drawRect(0, 0, buttonWidth, buttonHeight);
143
144 // draw icon
145 const QPixmap pixmap = icon().pixmap();
146 const int x = (buttonWidth - pixmap.width()) / 2;
147 const int y = (buttonHeight - pixmap.height()) / 2;
148 painter.drawPixmap(x, y, pixmap);
149 }
150
151 void BookmarkSelector::activateBookmark(QAction* action)
152 {
153 assert(action != 0);
154 m_selectedAddress = action->data().toString();
155
156 const KBookmark bookmark = selectedBookmark();
157 setPixmap(SmallIcon(bookmark.icon()));
158 emit bookmarkActivated(bookmark.url());
159 }
160
161 #include "bookmarkselector.moc"
162