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