]>
cloud.milkyroute.net Git - dolphin.git/blob - 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) *
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. *
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. *
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 ***************************************************************************/
21 #include "kfileplacesselector_p.h"
23 #include "kurlnavigator.h"
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kfileplacesmodel.h>
37 KFilePlacesSelector::KFilePlacesSelector(KUrlNavigator
* parent
, KFilePlacesModel
* placesModel
) :
40 m_urlNavigator(parent
),
41 m_placesModel(placesModel
)
43 setFocusPolicy(Qt::NoFocus
);
45 m_placesMenu
= new KMenu(this);
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
*)));
56 setMenu(m_placesMenu
);
59 KFilePlacesSelector::~KFilePlacesSelector()
63 void KFilePlacesSelector::updateMenu()
65 m_placesMenu
->clear();
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
),
73 m_placesMenu
->addAction(action
);
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);
86 void KFilePlacesSelector::updateSelection(const KUrl
& url
)
88 QModelIndex index
= m_placesModel
->closestItem(url
);
90 if (index
.isValid()) {
91 m_selectedItem
= index
.row();
92 setIcon(m_placesModel
->icon(index
));
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"));
102 KUrl
KFilePlacesSelector::selectedPlaceUrl() const
104 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
107 return m_placesModel
->url(index
);
112 QString
KFilePlacesSelector::selectedPlaceText() const
114 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
117 return m_placesModel
->text(index
);
122 QSize
KFilePlacesSelector::sizeHint() const
124 const int height
= KUrlButton::sizeHint().height();
125 return QSize(height
, height
);
128 void KFilePlacesSelector::paintEvent(QPaintEvent
* /*event*/)
130 QPainter
painter(this);
132 const int buttonWidth
= width();
133 const int buttonHeight
= height();
135 QColor backgroundColor
;
136 QColor foregroundColor
;
137 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
138 isDisplayHintEnabled(DraggedHint
);
140 backgroundColor
= KGlobalSettings::highlightColor();
141 foregroundColor
= KGlobalSettings::highlightedTextColor();
144 backgroundColor
= palette().brush(QPalette::Background
).color();
145 foregroundColor
= KGlobalSettings::buttonTextColor();
148 // dimm the colors if the parent view does not have the focus
149 const bool isActive
= m_urlNavigator
->isActive();
151 QColor
dimmColor(palette().brush(QPalette::Background
).color());
152 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
154 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
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
);
164 // draw button backround
165 painter
.setPen(Qt::NoPen
);
166 painter
.setBrush(backgroundColor
);
167 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
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
);
176 void KFilePlacesSelector::activatePlace(QAction
* action
)
179 m_selectedItem
= action
->data().toInt();
181 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
183 if (index
.isValid()) {
184 setIcon(m_placesModel
->icon(index
));
185 emit
placeActivated(m_placesModel
->url(index
));
189 #include "kfileplacesselector_p.moc"