┌   ┐
54
└   ┘

summaryrefslogtreecommitdiff
path: root/src/kfileplacesselector.cpp
diff options
context:
space:
mode:
authorKevin Ottens <[email protected]>2007-04-02 19:20:07 +0000
committerKevin Ottens <[email protected]>2007-04-02 19:20:07 +0000
commit03713b0ab408a42a20c9ce6a51d069b3246d2502 (patch)
tree1aff603918b502ae1927b74b5b668e4659268f51 /src/kfileplacesselector.cpp
parent78c312a0f33b15b34aed8ea7cf9e9c344dea1f87 (diff)
Rename all the URL navigator related classes to prepare their migration
in kdelibs. svn path=/trunk/KDE/kdebase/apps/; revision=649514
Diffstat (limited to 'src/kfileplacesselector.cpp')
-rw-r--r--src/kfileplacesselector.cpp189
1 files changed, 189 insertions, 0 deletions
diff --git a/src/kfileplacesselector.cpp b/src/kfileplacesselector.cpp
new file mode 100644
index 000000000..0052c3a1e
--- /dev/null
+++ b/src/kfileplacesselector.cpp
@@ -0,0 +1,189 @@
+/***************************************************************************
+ * Copyright (C) 2006 by Peter Penz ([email protected]) *
+ * Copyright (C) 2007 by Kevin Ottens ([email protected]) *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA *
+ ***************************************************************************/
+
+#include "kfileplacesselector_p.h"
+
+#include "kurlnavigator.h"
+
+#include <assert.h>
+
+#include <kiconloader.h>
+#include <kglobalsettings.h>
+#include <kfileplacesmodel.h>
+#include <kmenu.h>
+#include <kdebug.h>
+
+#include <QPainter>
+#include <QPixmap>
+#include <kicon.h>
+
+KFilePlacesSelector::KFilePlacesSelector(KUrlNavigator* parent, KFilePlacesModel* placesModel) :
+ KUrlButton(parent),
+ m_selectedItem(-1),
+ m_urlNavigator(parent),
+ m_placesModel(placesModel)
+{
+ setFocusPolicy(Qt::NoFocus);
+
+ m_placesMenu = new KMenu(this);
+
+ updateMenu();
+
+ connect(m_placesModel, SIGNAL(rowsInserted(const QModelIndex&, int, int)),
+ this, SLOT(updateMenu()));
+ connect(m_placesModel, SIGNAL(rowsRemoved(const QModelIndex&, int, int)),
+ this, SLOT(updateMenu()));
+ connect(m_placesMenu, SIGNAL(triggered(QAction*)),
+ this, SLOT(activatePlace(QAction*)));
+
+ setMenu(m_placesMenu);
+}
+
+KFilePlacesSelector::~KFilePlacesSelector()
+{
+}
+
+void KFilePlacesSelector::updateMenu()
+{
+ m_placesMenu->clear();
+
+ for (int i=0; i<m_placesModel->rowCount(); ++i) {
+ QModelIndex index = m_placesModel->index(i, 0);
+ QAction* action = new QAction(m_placesModel->icon(index),
+ m_placesModel->text(index),
+ m_placesMenu);
+ m_placesMenu->addAction(action);
+
+ action->setData(i);
+
+ if (i == m_selectedItem) {
+ //QPixmap pixmap = SmallIcon(bookmark.icon());
+ setIcon(m_placesModel->icon(index));
+ //setIconSize(pixmap.size());
+ //setMinimumWidth(pixmap.width() + 2);
+ }
+ }
+}
+
+void KFilePlacesSelector::updateSelection(const KUrl& url)
+{
+ QModelIndex index = m_placesModel->closestItem(url);
+
+ if (index.isValid()) {
+ m_selectedItem = index.row();
+ setIcon(m_placesModel->icon(index));
+ }
+ else {
+ m_selectedItem = -1;
+ // No bookmark has been found which matches to the given Url. Show
+ // a generic folder icon as pixmap for indication:
+ setIcon(KIcon("folder"));
+ }
+}
+
+KUrl KFilePlacesSelector::selectedPlaceUrl() const
+{
+ QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+ if (index.isValid())
+ return m_placesModel->url(index);
+ else
+ return KUrl();
+}
+
+QString KFilePlacesSelector::selectedPlaceText() const
+{
+ QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+ if (index.isValid())
+ return m_placesModel->text(index);
+ else
+ return QString();
+}
+
+QSize KFilePlacesSelector::sizeHint() const
+{
+ const int height = KUrlButton::sizeHint().height();
+ return QSize(height, height);
+}
+
+void KFilePlacesSelector::paintEvent(QPaintEvent* /*event*/)
+{
+ QPainter painter(this);
+
+ const int buttonWidth = width();
+ const int buttonHeight = height();
+
+ QColor backgroundColor;
+ QColor foregroundColor;
+ const bool isHighlighted = isDisplayHintEnabled(EnteredHint) ||
+ isDisplayHintEnabled(DraggedHint);
+ if (isHighlighted) {
+ backgroundColor = KGlobalSettings::highlightColor();
+ foregroundColor = KGlobalSettings::highlightedTextColor();
+ }
+ else {
+ backgroundColor = palette().brush(QPalette::Background).color();
+ foregroundColor = KGlobalSettings::buttonTextColor();
+ }
+
+ // dimm the colors if the parent view does not have the focus
+ const bool isActive = m_urlNavigator->isActive();
+ if (!isActive) {
+ QColor dimmColor(palette().brush(QPalette::Background).color());
+ foregroundColor = mixColors(foregroundColor, dimmColor);
+ if (isHighlighted) {
+ backgroundColor = mixColors(backgroundColor, dimmColor);
+ }
+ }
+
+ if (!(isDisplayHintEnabled(ActivatedHint) && isActive) && !isHighlighted) {
+ // dimm the foreground color by mixing it with the background
+ foregroundColor = mixColors(foregroundColor, backgroundColor);
+ painter.setPen(foregroundColor);
+ }
+
+ // draw button backround
+ painter.setPen(Qt::NoPen);
+ painter.setBrush(backgroundColor);
+ painter.drawRect(0, 0, buttonWidth, buttonHeight);
+
+ // draw icon
+ const QPixmap pixmap = icon().pixmap();
+ const int x = (buttonWidth - pixmap.width()) / 2;
+ const int y = (buttonHeight - pixmap.height()) / 2;
+ painter.drawPixmap(x, y, pixmap);
+}
+
+void KFilePlacesSelector::activatePlace(QAction* action)
+{
+ assert(action != 0);
+ m_selectedItem = action->data().toInt();
+
+ QModelIndex index = m_placesModel->index(m_selectedItem, 0);
+
+ if (index.isValid()) {
+ setIcon(m_placesModel->icon(index));
+ emit placeActivated(m_placesModel->url(index));
+ }
+}
+
+#include "kfileplacesselector_p.moc"
+