]>
cloud.milkyroute.net Git - dolphin.git/blob - src/bookmarkselector.cpp
1 /***************************************************************************
2 * Copyright (C) 2006 by Peter Penz *
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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <q3popupmenu.h>
27 #include <kiconloader.h>
28 #include <kglobalsettings.h>
29 #include <kbookmarkmanager.h>
31 #include "bookmarkselector.h"
32 #include "dolphinsettings.h"
33 #include "dolphinview.h"
34 #include "dolphinmainwindow.h"
35 #include "urlnavigator.h"
37 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
) :
41 setFocusPolicy(Qt::NoFocus
);
43 m_bookmarksMenu
= new Q3PopupMenu(this);
45 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
46 KBookmark bookmark
= root
.first();
48 while (!bookmark
.isNull()) {
49 m_bookmarksMenu
->insertItem(MainBarIcon(bookmark
.icon()),
52 if (i
== m_selectedIndex
) {
53 QPixmap pixmap
= SmallIcon(bookmark
.icon());
55 setMinimumWidth(pixmap
.width() + 2);
57 bookmark
= root
.next(bookmark
);
61 connect(m_bookmarksMenu
, SIGNAL(activated(int)),
62 this, SLOT(slotBookmarkActivated(int)));
64 setPopup(m_bookmarksMenu
);
67 BookmarkSelector::~BookmarkSelector()
71 void BookmarkSelector::updateSelection(const KUrl
& url
)
73 KBookmarkGroup root
= DolphinSettings::instance().bookmarkManager()->root();
74 KBookmark bookmark
= root
.first();
79 // Search the bookmark which is equal to the Url or at least is a parent Url.
80 // If there are more than one possible parent Url candidates, choose the bookmark
81 // which covers the bigger range of the Url.
83 while (!bookmark
.isNull()) {
84 const KUrl bookmarkUrl
= bookmark
.url();
85 if (bookmarkUrl
.isParentOf(url
)) {
86 const int length
= bookmarkUrl
.prettyUrl().length();
87 if (length
> maxLength
) {
89 setPixmap(SmallIcon(bookmark
.icon()));
93 bookmark
= root
.next(bookmark
);
97 if (m_selectedIndex
< 0) {
98 // No bookmark has been found which matches to the given Url. Show
99 // a generic folder icon as pixmap for indication:
100 setPixmap(SmallIcon("folder"));
104 KBookmark
BookmarkSelector::selectedBookmark() const
106 return DolphinSettings::instance().bookmark(m_selectedIndex
);
109 QSize
BookmarkSelector::sizeHint() const
111 const int height
= UrlButton::sizeHint().height();
112 return QSize(height
, height
);
115 void BookmarkSelector::paintEvent(QPaintEvent
* event
)
117 QPainter
painter(this);
119 const int buttonWidth
= width();
120 const int buttonHeight
= height();
122 QColor backgroundColor
;
123 QColor foregroundColor
;
124 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
125 isDisplayHintEnabled(DraggedHint
);
127 backgroundColor
= KGlobalSettings::highlightColor();
128 foregroundColor
= KGlobalSettings::highlightedTextColor();
131 backgroundColor
= colorGroup().background();
132 foregroundColor
= KGlobalSettings::buttonTextColor();
135 // dimm the colors if the parent view does not have the focus
136 const DolphinView
* parentView
= urlNavigator()->dolphinView();
137 const DolphinMainWindow
* dolphin
= parentView
->mainWindow();
139 const bool isActive
= (dolphin
->activeView() == parentView
);
141 QColor
dimmColor(colorGroup().background());
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::slotBookmarkActivated(int index
)
168 m_selectedIndex
= index
;
170 KBookmark bookmark
= selectedBookmark();
171 setPixmap(SmallIcon(bookmark
.icon()));
173 emit
bookmarkActivated(index
);
176 #include "bookmarkselector.moc"