]>
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 m_selectedIndex
= baseBookmarkIndex(url
);
77 if (m_selectedIndex
>= 0) {
78 KBookmark bookmark
= DolphinSettings::instance().bookmark(m_selectedIndex
);
79 setIcon(SmallIcon(bookmark
.icon()));
82 // No bookmark has been found which matches to the given Url. Show
83 // a generic folder icon as pixmap for indication:
84 setIcon(SmallIcon("folder"));
88 KBookmark
BookmarkSelector::selectedBookmark() const
90 return DolphinSettings::instance().bookmark(m_selectedIndex
);
93 QSize
BookmarkSelector::sizeHint() const
95 const int height
= UrlButton::sizeHint().height();
96 return QSize(height
, height
);
99 KBookmark
BookmarkSelector::baseBookmark(const KUrl
& url
)
101 const int index
= baseBookmarkIndex(url
);
102 return DolphinSettings::instance().bookmark(index
);
105 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
107 QPainter
painter(this);
109 const int buttonWidth
= width();
110 const int buttonHeight
= height();
112 QColor backgroundColor
;
113 QColor foregroundColor
;
114 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
115 isDisplayHintEnabled(DraggedHint
);
117 backgroundColor
= KGlobalSettings::highlightColor();
118 foregroundColor
= KGlobalSettings::highlightedTextColor();
121 backgroundColor
= palette().brush(QPalette::Background
).color();
122 foregroundColor
= KGlobalSettings::buttonTextColor();
125 // dimm the colors if the parent view does not have the focus
126 const bool isActive
= m_urlNavigator
->isActive();
128 QColor
dimmColor(palette().brush(QPalette::Background
).color());
129 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
131 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
135 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
136 // dimm the foreground color by mixing it with the background
137 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
138 painter
.setPen(foregroundColor
);
141 // draw button backround
142 painter
.setPen(Qt::NoPen
);
143 painter
.setBrush(backgroundColor
);
144 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
147 const QPixmap pixmap
= icon().pixmap();
148 const int x
= (buttonWidth
- pixmap
.width()) / 2;
149 const int y
= (buttonHeight
- pixmap
.height()) / 2;
150 painter
.drawPixmap(x
, y
, pixmap
);
153 void BookmarkSelector::activateBookmark(QAction
* action
)
156 m_selectedIndex
= action
->data().toInt();
158 const KBookmark bookmark
= selectedBookmark();
159 setPixmap(SmallIcon(bookmark
.icon()));
160 emit
bookmarkActivated(bookmark
.url());
163 int BookmarkSelector::baseBookmarkIndex(const KUrl
& url
)
165 int index
= -1; // return value
167 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
168 KBookmark bookmark
= root
.first();
172 // Search the bookmark which is equal to the Url or at least is a parent Url.
173 // If there are more than one possible parent Url candidates, choose the bookmark
174 // which covers the bigger range of the Url.
176 while (!bookmark
.isNull()) {
177 const KUrl bookmarkUrl
= bookmark
.url();
178 if (bookmarkUrl
.isParentOf(url
)) {
179 const int length
= bookmarkUrl
.prettyUrl().length();
180 if (length
> maxLength
) {
185 bookmark
= root
.next(bookmark
);
192 #include "bookmarkselector.moc"