]> cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
Make use of KFilePlaces instead of the bookmark system in the URL
[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 <kfileplacesmodel.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, KFilePlacesModel* placesModel) :
37 UrlButton(parent),
38 m_selectedItem(-1),
39 m_urlNavigator(parent),
40 m_placesModel(placesModel)
41 {
42 setFocusPolicy(Qt::NoFocus);
43
44 m_placesMenu = new KMenu(this);
45
46 updateMenu();
47
48 connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
49 this, SLOT(updateMenu()));
50 connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
51 this, SLOT(updateMenu()));
52 connect(m_placesMenu, SIGNAL(triggered(QAction*)),
53 this, SLOT(activatePlace(QAction*)));
54
55 setMenu(m_placesMenu);
56 }
57
58 BookmarkSelector::~BookmarkSelector()
59 {
60 }
61
62 void BookmarkSelector::updateMenu()
63 {
64 m_placesMenu->clear();
65
66 for (int i=0; i<m_placesModel->rowCount(); ++i) {
67 QModelIndex index = m_placesModel->index(i, 0);
68 QAction* action = new QAction(m_placesModel->icon(index),
69 m_placesModel->text(index),
70 m_placesMenu);
71 m_placesMenu->addAction(action);
72
73 action->setData(i);
74
75 if (i == m_selectedItem) {
76 //QPixmap pixmap = SmallIcon(bookmark.icon());
77 setIcon(m_placesModel->icon(index));
78 //setIconSize(pixmap.size());
79 //setMinimumWidth(pixmap.width() + 2);
80 }
81 }
82 }
83
84 void BookmarkSelector::updateSelection(const KUrl& url)
85 {
86 QModelIndex index = m_placesModel->closestItem(url);
87
88 if (index.isValid()) {
89 m_selectedItem = index.row();
90 setIcon(m_placesModel->icon(index));
91 }
92 else {
93 m_selectedItem = -1;
94 // No bookmark has been found which matches to the given Url. Show
95 // a generic folder icon as pixmap for indication:
96 setIcon(KIcon("folder"));
97 }
98 }
99
100 KUrl BookmarkSelector::selectedPlaceUrl() const
101 {
102 QModelIndex index = m_placesModel->index(m_selectedItem, 0);
103
104 if (index.isValid())
105 return m_placesModel->url(index);
106 else
107 return KUrl();
108 }
109
110 QString BookmarkSelector::selectedPlaceText() const
111 {
112 QModelIndex index = m_placesModel->index(m_selectedItem, 0);
113
114 if (index.isValid())
115 return m_placesModel->text(index);
116 else
117 return QString();
118 }
119
120 QSize BookmarkSelector::sizeHint() const
121 {
122 const int height = UrlButton::sizeHint().height();
123 return QSize(height, height);
124 }
125
126 void BookmarkSelector::paintEvent(QPaintEvent* /*event*/)
127 {
128 QPainter painter(this);
129
130 const int buttonWidth = width();
131 const int buttonHeight = height();
132
133 QColor backgroundColor;
134 QColor foregroundColor;
135 const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
136 isDisplayHintEnabled(DraggedHint);
137 if (isHighlighted) {
138 backgroundColor = KGlobalSettings::highlightColor();
139 foregroundColor = KGlobalSettings::highlightedTextColor();
140 }
141 else {
142 backgroundColor = palette().brush(QPalette::Background).color();
143 foregroundColor = KGlobalSettings::buttonTextColor();
144 }
145
146 // dimm the colors if the parent view does not have the focus
147 const bool isActive = m_urlNavigator->isActive();
148 if (!isActive) {
149 QColor dimmColor(palette().brush(QPalette::Background).color());
150 foregroundColor = mixColors(foregroundColor, dimmColor);
151 if (isHighlighted) {
152 backgroundColor = mixColors(backgroundColor, dimmColor);
153 }
154 }
155
156 if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
157 // dimm the foreground color by mixing it with the background
158 foregroundColor = mixColors(foregroundColor, backgroundColor);
159 painter.setPen(foregroundColor);
160 }
161
162 // draw button backround
163 painter.setPen(Qt::NoPen);
164 painter.setBrush(backgroundColor);
165 painter.drawRect(0, 0, buttonWidth, buttonHeight);
166
167 // draw icon
168 const QPixmap pixmap = icon().pixmap();
169 const int x = (buttonWidth - pixmap.width()) / 2;
170 const int y = (buttonHeight - pixmap.height()) / 2;
171 painter.drawPixmap(x, y, pixmap);
172 }
173
174 void BookmarkSelector::activatePlace(QAction* action)
175 {
176 assert(action != 0);
177 m_selectedItem = action->data().toInt();
178
179 QModelIndex index = m_placesModel->index(m_selectedItem, 0);
180
181 if (index.isValid()) {
182 setIcon(m_placesModel->icon(index));
183 emit placeActivated(m_placesModel->url(index));
184 }
185 }
186
187 #include "bookmarkselector.moc"
188