]>
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"
35 #include "urlnavigator.h"
37 BookmarkSelector::BookmarkSelector(URLNavigator
* parent
) :
41 setFocusPolicy(QWidget::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 void BookmarkSelector::drawButton(QPainter
* painter
)
111 const int buttonWidth
= width();
112 const int buttonHeight
= height();
114 QColor backgroundColor
;
115 QColor foregroundColor
;
116 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
117 isDisplayHintEnabled(DraggedHint
);
119 backgroundColor
= KGlobalSettings::highlightColor();
120 foregroundColor
= KGlobalSettings::highlightedTextColor();
123 backgroundColor
= colorGroup().background();
124 foregroundColor
= KGlobalSettings::buttonTextColor();
127 // dimm the colors if the parent view does not have the focus
128 const DolphinView
* parentView
= urlNavigator()->dolphinView();
129 const Dolphin
& dolphin
= Dolphin::mainWin();
131 const bool isActive
= (dolphin
.activeView() == parentView
);
133 QColor
dimmColor(colorGroup().background());
134 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
136 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
140 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
141 // dimm the foreground color by mixing it with the background
142 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
143 painter
->setPen(foregroundColor
);
146 // draw button backround
147 painter
->setPen(NoPen
);
148 painter
->setBrush(backgroundColor
);
149 painter
->drawRect(0, 0, buttonWidth
, buttonHeight
);
152 const QPixmap
* icon
= pixmap();
154 const int x
= (buttonWidth
- icon
->width()) / 2;
155 const int y
= (buttonHeight
- icon
->height()) / 2;
156 painter
->drawPixmap(x
, y
, *icon
);
160 void BookmarkSelector::slotBookmarkActivated(int index
)
162 m_selectedIndex
= index
;
164 KBookmark bookmark
= selectedBookmark();
165 setPixmap(SmallIcon(bookmark
.icon()));
167 emit
bookmarkActivated(index
);
170 #include "bookmarkselector.moc"