]>
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 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
),
72 m_placesMenu
->addAction(action
);
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);
85 void KFilePlacesSelector::updateSelection(const KUrl
& url
)
87 QModelIndex index
= m_placesModel
->closestItem(url
);
89 if (index
.isValid()) {
90 m_selectedItem
= index
.row();
91 setIcon(m_placesModel
->icon(index
));
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"));
101 KUrl
KFilePlacesSelector::selectedPlaceUrl() const
103 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
106 return m_placesModel
->url(index
);
111 QString
KFilePlacesSelector::selectedPlaceText() const
113 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
116 return m_placesModel
->text(index
);
121 QSize
KFilePlacesSelector::sizeHint() const
123 const int height
= KUrlButton::sizeHint().height();
124 return QSize(height
, height
);
127 void KFilePlacesSelector::paintEvent(QPaintEvent
* /*event*/)
129 QPainter
painter(this);
131 const int buttonWidth
= width();
132 const int buttonHeight
= height();
134 QColor backgroundColor
;
135 QColor foregroundColor
;
136 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
137 isDisplayHintEnabled(DraggedHint
);
139 backgroundColor
= KGlobalSettings::highlightColor();
140 foregroundColor
= KGlobalSettings::highlightedTextColor();
143 backgroundColor
= palette().brush(QPalette::Background
).color();
144 foregroundColor
= KGlobalSettings::buttonTextColor();
147 // dimm the colors if the parent view does not have the focus
148 const bool isActive
= m_urlNavigator
->isActive();
150 QColor
dimmColor(palette().brush(QPalette::Background
).color());
151 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
153 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
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
);
163 // draw button backround
164 painter
.setPen(Qt::NoPen
);
165 painter
.setBrush(backgroundColor
);
166 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
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
);
175 void KFilePlacesSelector::activatePlace(QAction
* action
)
178 m_selectedItem
= action
->data().toInt();
180 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
182 if (index
.isValid()) {
183 setIcon(m_placesModel
->icon(index
));
184 emit
placeActivated(m_placesModel
->url(index
));
188 #include "kfileplacesselector_p.moc"