]>
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 "urlnavigator.h"
26 #include <kiconloader.h>
27 #include <kglobalsettings.h>
28 #include <kbookmarkmanager.h>
35 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
, KBookmarkManager
* bookmarkManager
) :
38 m_urlNavigator(parent
),
39 m_bookmarkManager(bookmarkManager
)
41 setFocusPolicy(Qt::NoFocus
);
43 m_bookmarksMenu
= new KMenu(this);
45 KBookmarkGroup root
= m_bookmarkManager
->root();
46 KBookmark bookmark
= root
.first();
48 while (!bookmark
.isNull()) {
49 QAction
* action
= new QAction(MainBarIcon(bookmark
.icon()),
52 m_bookmarksMenu
->addAction(action
);
53 QString address
= QChar('/');
54 address
+= QString::number(i
);
55 action
->setData(address
);
56 if (address
== m_selectedAddress
) {
57 QPixmap pixmap
= SmallIcon(bookmark
.icon());
58 setIcon(QIcon(pixmap
));
59 setIconSize(pixmap
.size());
60 setMinimumWidth(pixmap
.width() + 2);
62 bookmark
= root
.next(bookmark
);
66 connect(m_bookmarksMenu
, SIGNAL(triggered(QAction
*)),
67 this, SLOT(activateBookmark(QAction
*)));
69 setMenu(m_bookmarksMenu
);
72 BookmarkSelector::~BookmarkSelector()
76 void BookmarkSelector::updateSelection(const KUrl
& url
)
78 KBookmark bookmark
= baseBookmark(m_bookmarkManager
, url
);
79 if (!bookmark
.isNull()) {
80 m_selectedAddress
= bookmark
.address();
81 setIcon(SmallIcon(bookmark
.icon()));
84 m_selectedAddress
= QString();
85 // No bookmark has been found which matches to the given Url. Show
86 // a generic folder icon as pixmap for indication:
87 setIcon(SmallIcon("folder"));
91 KBookmark
BookmarkSelector::selectedBookmark() const
93 return m_bookmarkManager
->findByAddress(m_selectedAddress
);
96 QSize
BookmarkSelector::sizeHint() const
98 const int height
= UrlButton::sizeHint().height();
99 return QSize(height
, height
);
102 KBookmark
BookmarkSelector::baseBookmark(KBookmarkManager
* bookmarkManager
, const KUrl
& url
)
104 const KBookmarkGroup root
= bookmarkManager
->root();
105 KBookmark bookmark
= root
.first();
106 KBookmark foundBookmark
;
110 // Search the bookmark which is equal to the Url or at least is a parent Url.
111 // If there are more than one possible parent Url candidates, choose the bookmark
112 // which covers the bigger range of the Url.
114 while (!bookmark
.isNull()) {
115 const KUrl bookmarkUrl
= bookmark
.url();
116 if (bookmarkUrl
.isParentOf(url
)) {
117 const int length
= bookmarkUrl
.prettyUrl().length();
118 if (length
> maxLength
) {
119 foundBookmark
= bookmark
;
123 bookmark
= root
.next(bookmark
);
127 return foundBookmark
;
130 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
132 QPainter
painter(this);
134 const int buttonWidth
= width();
135 const int buttonHeight
= height();
137 QColor backgroundColor
;
138 QColor foregroundColor
;
139 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
140 isDisplayHintEnabled(DraggedHint
);
142 backgroundColor
= KGlobalSettings::highlightColor();
143 foregroundColor
= KGlobalSettings::highlightedTextColor();
146 backgroundColor
= palette().brush(QPalette::Background
).color();
147 foregroundColor
= KGlobalSettings::buttonTextColor();
150 // dimm the colors if the parent view does not have the focus
151 const bool isActive
= m_urlNavigator
->isActive();
153 QColor
dimmColor(palette().brush(QPalette::Background
).color());
154 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
156 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
160 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
161 // dimm the foreground color by mixing it with the background
162 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
163 painter
.setPen(foregroundColor
);
166 // draw button backround
167 painter
.setPen(Qt::NoPen
);
168 painter
.setBrush(backgroundColor
);
169 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
172 const QPixmap pixmap
= icon().pixmap();
173 const int x
= (buttonWidth
- pixmap
.width()) / 2;
174 const int y
= (buttonHeight
- pixmap
.height()) / 2;
175 painter
.drawPixmap(x
, y
, pixmap
);
178 void BookmarkSelector::activateBookmark(QAction
* action
)
181 m_selectedAddress
= action
->data().toString();
183 const KBookmark bookmark
= selectedBookmark();
184 setPixmap(SmallIcon(bookmark
.icon()));
185 emit
bookmarkActivated(bookmark
.url());
188 #include "bookmarkselector.moc"