]>
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"
23 #include <q3popupmenu.h>
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
31 #include "dolphinsettings.h"
32 #include "urlnavigator.h"
34 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
) :
37 m_urlNavigator(parent
)
39 setFocusPolicy(Qt::NoFocus
);
41 m_bookmarksMenu
= new Q3PopupMenu(this);
43 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
44 KBookmark bookmark
= root
.first();
46 while (!bookmark
.isNull()) {
47 m_bookmarksMenu
->insertItem(MainBarIcon(bookmark
.icon()),
50 if (i
== m_selectedIndex
) {
51 QPixmap pixmap
= SmallIcon(bookmark
.icon());
52 setIcon(QIcon(pixmap
));
53 setIconSize(pixmap
.size());
54 setMinimumWidth(pixmap
.width() + 2);
56 bookmark
= root
.next(bookmark
);
60 connect(m_bookmarksMenu
, SIGNAL(activated(int)),
61 this, SLOT(slotBookmarkActivated(int)));
63 setMenu(m_bookmarksMenu
);
66 BookmarkSelector::~BookmarkSelector()
70 void BookmarkSelector::updateSelection(const KUrl
& url
)
72 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
73 KBookmark bookmark
= root
.first();
78 // Search the bookmark which is equal to the Url or at least is a parent Url.
79 // If there are more than one possible parent Url candidates, choose the bookmark
80 // which covers the bigger range of the Url.
82 while (!bookmark
.isNull()) {
83 const KUrl bookmarkUrl
= bookmark
.url();
84 if (bookmarkUrl
.isParentOf(url
)) {
85 const int length
= bookmarkUrl
.prettyUrl().length();
86 if (length
> maxLength
) {
88 setIcon(SmallIcon(bookmark
.icon()));
92 bookmark
= root
.next(bookmark
);
96 if (m_selectedIndex
< 0) {
97 // No bookmark has been found which matches to the given Url. Show
98 // a generic folder icon as pixmap for indication:
99 setIcon(SmallIcon("folder"));
103 KBookmark
BookmarkSelector::selectedBookmark() const
105 return DolphinSettings::instance().bookmark(m_selectedIndex
);
108 QSize
BookmarkSelector::sizeHint() const
110 const int height
= UrlButton::sizeHint().height();
111 return QSize(height
, height
);
114 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
116 QPainter
painter(this);
118 const int buttonWidth
= width();
119 const int buttonHeight
= height();
121 QColor backgroundColor
;
122 QColor foregroundColor
;
123 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
124 isDisplayHintEnabled(DraggedHint
);
126 backgroundColor
= KGlobalSettings::highlightColor();
127 foregroundColor
= KGlobalSettings::highlightedTextColor();
130 backgroundColor
= palette().brush(QPalette::Background
).color();
131 foregroundColor
= KGlobalSettings::buttonTextColor();
134 // dimm the colors if the parent view does not have the focus
135 const bool isActive
= m_urlNavigator
->isActive();
137 QColor
dimmColor(palette().brush(QPalette::Background
).color());
138 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
140 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
144 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
145 // dimm the foreground color by mixing it with the background
146 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
147 painter
.setPen(foregroundColor
);
150 // draw button backround
151 painter
.setPen(Qt::NoPen
);
152 painter
.setBrush(backgroundColor
);
153 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
156 const QPixmap pixmap
= icon().pixmap();
157 const int x
= (buttonWidth
- pixmap
.width()) / 2;
158 const int y
= (buttonHeight
- pixmap
.height()) / 2;
159 painter
.drawPixmap(x
, y
, pixmap
);
162 void BookmarkSelector::slotBookmarkActivated(int index
)
164 m_selectedIndex
= index
;
166 const KBookmark bookmark
= selectedBookmark();
167 setPixmap(SmallIcon(bookmark
.icon()));
168 emit
bookmarkActivated(bookmark
.url());
171 #include "bookmarkselector.moc"