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