]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz (peter.penz@gmx.at) *
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. *
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. *
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 ***************************************************************************/
20 #include "bookmarkselector.h"
22 #include "urlnavigator.h"
26 #include <kiconloader.h>
27 #include <kglobalsettings.h>
28 #include <kfileplacesmodel.h>
36 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
, KFilePlacesModel
* placesModel
) :
39 m_urlNavigator(parent
),
40 m_placesModel(placesModel
)
42 setFocusPolicy(Qt::NoFocus
);
44 m_placesMenu
= new KMenu(this);
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
*)));
55 setMenu(m_placesMenu
);
58 BookmarkSelector::~BookmarkSelector()
62 void BookmarkSelector::updateMenu()
64 m_placesMenu
->clear();
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
),
71 m_placesMenu
->addAction(action
);
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);
84 void BookmarkSelector::updateSelection(const KUrl
& url
)
86 QModelIndex index
= m_placesModel
->closestItem(url
);
88 if (index
.isValid()) {
89 m_selectedItem
= index
.row();
90 setIcon(m_placesModel
->icon(index
));
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"));
100 KUrl
BookmarkSelector::selectedPlaceUrl() const
102 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
105 return m_placesModel
->url(index
);
110 QString
BookmarkSelector::selectedPlaceText() const
112 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
115 return m_placesModel
->text(index
);
120 QSize
BookmarkSelector::sizeHint() const
122 const int height
= UrlButton::sizeHint().height();
123 return QSize(height
, height
);
126 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
128 QPainter
painter(this);
130 const int buttonWidth
= width();
131 const int buttonHeight
= height();
133 QColor backgroundColor
;
134 QColor foregroundColor
;
135 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
136 isDisplayHintEnabled(DraggedHint
);
138 backgroundColor
= KGlobalSettings::highlightColor();
139 foregroundColor
= KGlobalSettings::highlightedTextColor();
142 backgroundColor
= palette().brush(QPalette::Background
).color();
143 foregroundColor
= KGlobalSettings::buttonTextColor();
146 // dimm the colors if the parent view does not have the focus
147 const bool isActive
= m_urlNavigator
->isActive();
149 QColor
dimmColor(palette().brush(QPalette::Background
).color());
150 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
152 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
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
);
162 // draw button backround
163 painter
.setPen(Qt::NoPen
);
164 painter
.setBrush(backgroundColor
);
165 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
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
);
174 void BookmarkSelector::activatePlace(QAction
* action
)
177 m_selectedItem
= action
->data().toInt();
179 QModelIndex index
= m_placesModel
->index(m_selectedItem
, 0);
181 if (index
.isValid()) {
182 setIcon(m_placesModel
->icon(index
));
183 emit
placeActivated(m_placesModel
->url(index
));
187 #include "bookmarkselector.moc"