]>
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 "dolphinsettings.h"
23 #include "urlnavigator.h"
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
36 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
) :
39 m_urlNavigator(parent
)
41 setFocusPolicy(Qt::NoFocus
);
43 m_bookmarksMenu
= new KMenu(this);
45 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
46 KBookmark bookmark
= root
.first();
48 while (!bookmark
.isNull()) {
49 QAction
* action
= new QAction(MainBarIcon(bookmark
.icon()),
53 m_bookmarksMenu
->addAction(action
);
54 if (i
== m_selectedIndex
) {
55 QPixmap pixmap
= SmallIcon(bookmark
.icon());
56 setIcon(QIcon(pixmap
));
57 setIconSize(pixmap
.size());
58 setMinimumWidth(pixmap
.width() + 2);
60 bookmark
= root
.next(bookmark
);
64 connect(m_bookmarksMenu
, SIGNAL(triggered(QAction
*)),
65 this, SLOT(activateBookmark(QAction
*)));
67 setMenu(m_bookmarksMenu
);
70 BookmarkSelector::~BookmarkSelector()
74 void BookmarkSelector::updateSelection(const KUrl
& url
)
76 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
77 KBookmark bookmark
= root
.first();
82 // Search the bookmark which is equal to the Url or at least is a parent Url.
83 // If there are more than one possible parent Url candidates, choose the bookmark
84 // which covers the bigger range of the Url.
86 while (!bookmark
.isNull()) {
87 const KUrl bookmarkUrl
= bookmark
.url();
88 if (bookmarkUrl
.isParentOf(url
)) {
89 const int length
= bookmarkUrl
.prettyUrl().length();
90 if (length
> maxLength
) {
92 setIcon(SmallIcon(bookmark
.icon()));
96 bookmark
= root
.next(bookmark
);
100 if (m_selectedIndex
< 0) {
101 // No bookmark has been found which matches to the given Url. Show
102 // a generic folder icon as pixmap for indication:
103 setIcon(SmallIcon("folder"));
107 KBookmark
BookmarkSelector::selectedBookmark() const
109 return DolphinSettings::instance().bookmark(m_selectedIndex
);
112 QSize
BookmarkSelector::sizeHint() const
114 const int height
= UrlButton::sizeHint().height();
115 return QSize(height
, height
);
118 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
120 QPainter
painter(this);
122 const int buttonWidth
= width();
123 const int buttonHeight
= height();
125 QColor backgroundColor
;
126 QColor foregroundColor
;
127 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
128 isDisplayHintEnabled(DraggedHint
);
130 backgroundColor
= KGlobalSettings::highlightColor();
131 foregroundColor
= KGlobalSettings::highlightedTextColor();
134 backgroundColor
= palette().brush(QPalette::Background
).color();
135 foregroundColor
= KGlobalSettings::buttonTextColor();
138 // dimm the colors if the parent view does not have the focus
139 const bool isActive
= m_urlNavigator
->isActive();
141 QColor
dimmColor(palette().brush(QPalette::Background
).color());
142 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
144 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
148 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
149 // dimm the foreground color by mixing it with the background
150 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
151 painter
.setPen(foregroundColor
);
154 // draw button backround
155 painter
.setPen(Qt::NoPen
);
156 painter
.setBrush(backgroundColor
);
157 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
160 const QPixmap pixmap
= icon().pixmap();
161 const int x
= (buttonWidth
- pixmap
.width()) / 2;
162 const int y
= (buttonHeight
- pixmap
.height()) / 2;
163 painter
.drawPixmap(x
, y
, pixmap
);
166 void BookmarkSelector::activateBookmark(QAction
* action
)
169 m_selectedIndex
= action
->data().toInt();
171 const KBookmark bookmark
= selectedBookmark();
172 setPixmap(SmallIcon(bookmark
.icon()));
173 emit
bookmarkActivated(bookmark
.url());
176 #include "bookmarkselector.moc"