]>
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>
36 BookmarkSelector::BookmarkSelector(UrlNavigator
* parent
, KBookmarkManager
* bookmarkManager
) :
39 m_urlNavigator(parent
),
40 m_bookmarkManager(bookmarkManager
)
42 setFocusPolicy(Qt::NoFocus
);
44 m_bookmarksMenu
= new KMenu(this);
46 KBookmarkGroup root
= m_bookmarkManager
->root();
47 KBookmark bookmark
= root
.first();
49 while (!bookmark
.isNull()) {
50 QAction
* action
= new QAction(MainBarIcon(bookmark
.icon()),
53 m_bookmarksMenu
->addAction(action
);
54 QString address
= QChar('/');
55 address
+= QString::number(i
);
56 action
->setData(address
);
57 if (address
== m_selectedAddress
) {
58 QPixmap pixmap
= SmallIcon(bookmark
.icon());
59 setIcon(QIcon(pixmap
));
60 setIconSize(pixmap
.size());
61 setMinimumWidth(pixmap
.width() + 2);
63 bookmark
= root
.next(bookmark
);
67 connect(m_bookmarksMenu
, SIGNAL(triggered(QAction
*)),
68 this, SLOT(activateBookmark(QAction
*)));
70 setMenu(m_bookmarksMenu
);
73 BookmarkSelector::~BookmarkSelector()
77 void BookmarkSelector::updateSelection(const KUrl
& url
)
79 KBookmark bookmark
= m_bookmarkManager
->root().closestBookmark(url
);
80 if (!bookmark
.isNull()) {
81 m_selectedAddress
= bookmark
.address();
82 setIcon(KIcon(bookmark
.icon()));
85 m_selectedAddress
= QString();
86 // No bookmark has been found which matches to the given Url. Show
87 // a generic folder icon as pixmap for indication:
88 setIcon(KIcon("folder"));
92 KBookmark
BookmarkSelector::selectedBookmark() const
94 return m_bookmarkManager
->findByAddress(m_selectedAddress
);
97 QSize
BookmarkSelector::sizeHint() const
99 const int height
= UrlButton::sizeHint().height();
100 return QSize(height
, height
);
103 void BookmarkSelector::paintEvent(QPaintEvent
* /*event*/)
105 QPainter
painter(this);
107 const int buttonWidth
= width();
108 const int buttonHeight
= height();
110 QColor backgroundColor
;
111 QColor foregroundColor
;
112 const bool isHighlighted
= isDisplayHintEnabled(EnteredHint
) ||
113 isDisplayHintEnabled(DraggedHint
);
115 backgroundColor
= KGlobalSettings::highlightColor();
116 foregroundColor
= KGlobalSettings::highlightedTextColor();
119 backgroundColor
= palette().brush(QPalette::Background
).color();
120 foregroundColor
= KGlobalSettings::buttonTextColor();
123 // dimm the colors if the parent view does not have the focus
124 const bool isActive
= m_urlNavigator
->isActive();
126 QColor
dimmColor(palette().brush(QPalette::Background
).color());
127 foregroundColor
= mixColors(foregroundColor
, dimmColor
);
129 backgroundColor
= mixColors(backgroundColor
, dimmColor
);
133 if (!(isDisplayHintEnabled(ActivatedHint
) && isActive
) && !isHighlighted
) {
134 // dimm the foreground color by mixing it with the background
135 foregroundColor
= mixColors(foregroundColor
, backgroundColor
);
136 painter
.setPen(foregroundColor
);
139 // draw button backround
140 painter
.setPen(Qt::NoPen
);
141 painter
.setBrush(backgroundColor
);
142 painter
.drawRect(0, 0, buttonWidth
, buttonHeight
);
145 const QPixmap pixmap
= icon().pixmap();
146 const int x
= (buttonWidth
- pixmap
.width()) / 2;
147 const int y
= (buttonHeight
- pixmap
.height()) / 2;
148 painter
.drawPixmap(x
, y
, pixmap
);
151 void BookmarkSelector::activateBookmark(QAction
* action
)
154 m_selectedAddress
= action
->data().toString();
156 const KBookmark bookmark
= selectedBookmark();
157 setPixmap(SmallIcon(bookmark
.icon()));
158 emit
bookmarkActivated(bookmark
.url());
161 #include "bookmarkselector.moc"