]>
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
= m_bookmarkManager
->root().closestBookmark(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 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
104 QPainter
painter(this);
106 const int buttonWidth
= width();
107 const int buttonHeight
= height();
109 QColor backgroundColor
;
110 QColor foregroundColor
;
111 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
112 isDisplayHintEnabled(DraggedHint
);
114 backgroundColor
= KGlobalSettings::highlightColor();
115 foregroundColor
= KGlobalSettings::highlightedTextColor();
118 backgroundColor
= palette().brush(QPalette::Background
).color();
119 foregroundColor
= KGlobalSettings::buttonTextColor();
122 // dimm the colors if the parent view does not have the focus
123 const bool isActive
= m_urlNavigator
->isActive();
125 QColor
dimmColor(palette().brush(QPalette::Background
).color());
126 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
128 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
132 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
133 // dimm the foreground color by mixing it with the background
134 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
135 painter
.setPen(foregroundColor
);
138 // draw button backround
139 painter
.setPen(Qt::NoPen
);
140 painter
.setBrush(backgroundColor
);
141 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
144 const QPixmap pixmap
= icon().pixmap();
145 const int x
= (buttonWidth
- pixmap
.width()) / 2;
146 const int y
= (buttonHeight
- pixmap
.height()) / 2;
147 painter
.drawPixmap(x
, y
, pixmap
);
150 void BookmarkSelector::activateBookmark(QAction
* action
)
153 m_selectedAddress
= action
->data().toString();
155 const KBookmark bookmark
= selectedBookmark();
156 setPixmap(SmallIcon(bookmark
.icon()));
157 emit
bookmarkActivated(bookmark
.url());
160 #include "bookmarkselector.moc"