Bring back basic bookmark support for the Places Panel

The folders-panel signals have been adjusted too for consistency.
This commit is contained in:
Peter Penz 2012-04-21 23:27:53 +02:00
parent e3f46c3b20
commit 007907be20
10 changed files with 173 additions and 67 deletions

View file

@ -508,6 +508,12 @@ void DolphinMainWindow::openNewTab(const KUrl& url)
}
}
void DolphinMainWindow::openNewActivatedTab(const KUrl& url)
{
openNewTab(url);
m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
}
void DolphinMainWindow::activateNextTab()
{
if (m_viewTab.count() >= 2) {
@ -1260,16 +1266,6 @@ void DolphinMainWindow::slotTabMoved(int from, int to)
m_tabIndex = m_tabBar->currentIndex();
}
void DolphinMainWindow::handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons)
{
if (buttons & Qt::MidButton) {
openNewTab(url);
m_tabBar->setCurrentIndex(m_viewTab.count() - 1);
} else {
changeUrl(url);
}
}
void DolphinMainWindow::slotTestCanDecode(const QDragMoveEvent* event, bool& canDecode)
{
canDecode = KUrl::List::canDecode(event->mimeData());
@ -1781,8 +1777,10 @@ void DolphinMainWindow::setupDockWidgets()
addDockWidget(Qt::LeftDockWidgetArea, foldersDock);
connect(this, SIGNAL(urlChanged(KUrl)),
foldersPanel, SLOT(setUrl(KUrl)));
connect(foldersPanel, SIGNAL(changeUrl(KUrl,Qt::MouseButtons)),
this, SLOT(handlePlacesClick(KUrl,Qt::MouseButtons)));
connect(foldersPanel, SIGNAL(folderActivated(KUrl)),
this, SLOT(changeUrl(KUrl)));
connect(foldersPanel, SIGNAL(folderMiddleClicked(KUrl)),
this, SLOT(openNewActivatedTab(KUrl)));
// Setup "Terminal"
#ifndef Q_OS_WIN
@ -1856,8 +1854,10 @@ void DolphinMainWindow::setupDockWidgets()
createPanelAction(KIcon("bookmarks"), Qt::Key_F9, placesAction, "show_places_panel");
addDockWidget(Qt::LeftDockWidgetArea, placesDock);
//connect(placesPanel, SIGNAL(urlChanged(KUrl,Qt::MouseButtons)),
// this, SLOT(handlePlacesClick(KUrl,Qt::MouseButtons)));
connect(placesPanel, SIGNAL(placeActivated(KUrl)),
this, SLOT(changeUrl(KUrl)));
connect(placesPanel, SIGNAL(placeMiddleClicked(KUrl)),
this, SLOT(openNewActivatedTab(KUrl)));
connect(this, SIGNAL(urlChanged(KUrl)),
placesPanel, SLOT(setUrl(KUrl)));
connect(placesDock, SIGNAL(visibilityChanged(bool)),

View file

@ -350,10 +350,16 @@ private slots:
void openNewTab();
/**
* Opens a new tab showing the URL \a url.
* Opens a new tab in the background showing the URL \a url.
*/
void openNewTab(const KUrl& url);
/**
* Opens a new tab showing the URL \a url and activates
* the tab.
*/
void openNewActivatedTab(const KUrl& url);
void activateNextTab();
void activatePrevTab();
@ -404,13 +410,6 @@ private slots:
*/
void slotTabMoved(int from, int to);
/**
* Handles a click on a places item: if the middle mouse button is
* clicked, a new tab is opened for \a url, otherwise the current
* view is replaced by \a url.
*/
void handlePlacesClick(const KUrl& url, Qt::MouseButtons buttons);
/**
* Is connected to the KTabBar signal testCanDecode() and adjusts
* the output parameter \a accept.

View file

@ -19,34 +19,33 @@
#include "kstandarditem.h"
#include <KDebug>
KStandardItem::KStandardItem(KStandardItem* parent) :
m_text(),
m_icon(),
m_group(),
m_parent(parent),
m_children(),
m_model(0)
m_model(0),
m_data()
{
}
KStandardItem::KStandardItem(const QString& text, KStandardItem* parent) :
m_text(text),
m_icon(),
m_group(),
m_parent(parent),
m_children(),
m_model(0)
m_model(0),
m_data()
{
setText(text);
}
KStandardItem::KStandardItem(const QIcon& icon, const QString& text, KStandardItem* parent) :
m_text(text),
m_icon(icon),
m_group(),
m_parent(parent),
m_children(),
m_model(0)
m_model(0),
m_data()
{
setIcon(icon);
setText(text);
}
KStandardItem::~KStandardItem()
@ -55,32 +54,42 @@ KStandardItem::~KStandardItem()
void KStandardItem::setText(const QString& text)
{
m_text = text;
m_data.insert("text", text);
}
QString KStandardItem::text() const
{
return m_text;
return m_data["text"].toString();
}
void KStandardItem::setIcon(const QIcon& icon)
{
m_icon = icon;
m_data.insert("iconName", icon.name());
}
QIcon KStandardItem::icon() const
{
return m_icon;
return QIcon(m_data["iconName"].toString());
}
void KStandardItem::setGroup(const QString& group)
{
m_group = group;
m_data.insert("group", group);
}
QString KStandardItem::group() const
{
return m_group;
return m_data["group"].toString();
}
void KStandardItem::setDataValue(const QByteArray& role, const QVariant& value)
{
m_data.insert(role, value);
}
QVariant KStandardItem::dataValue(const QByteArray& role) const
{
return m_data[role];
}
void KStandardItem::setParent(KStandardItem* parent)
@ -94,6 +103,11 @@ KStandardItem* KStandardItem::parent() const
return m_parent;
}
QHash<QByteArray, QVariant> KStandardItem::data() const
{
return m_data;
}
QList<KStandardItem*> KStandardItem::children() const
{
return m_children;

View file

@ -22,16 +22,20 @@
#include <libdolphin_export.h>
#include <QByteArray>
#include <QHash>
#include <QIcon>
#include <QList>
#include <QVariant>
class KStandardItemModel;
/**
* @brief Represents and item of KStandardItemModel.
*
* Provides setter- and getter-methods for most commonly
* used properties.
* Provides setter- and getter-methods for the most commonly
* used roles. It is possible to assign values for custom
* roles by using setDataValue().
*/
class LIBDOLPHINPRIVATE_EXPORT KStandardItem
{
@ -42,28 +46,40 @@ public:
KStandardItem(const QIcon& icon, const QString& text, KStandardItem* parent = 0);
virtual ~KStandardItem();
/**
* Sets the text for the "text"-role.
*/
void setText(const QString& text);
QString text() const;
/**
* Sets the icon for the "iconName"-role.
*/
void setIcon(const QIcon& icon);
QIcon icon() const;
/**
* Sets the group for the "group"-role.
*/
void setGroup(const QString& group);
QString group() const;
void setDataValue(const QByteArray& role, const QVariant& value);
QVariant dataValue(const QByteArray& role) const;
void setParent(KStandardItem* parent);
KStandardItem* parent() const;
QHash<QByteArray, QVariant> data() const;
QList<KStandardItem*> children() const;
private:
QString m_text;
QIcon m_icon;
QString m_group;
KStandardItem* m_parent;
QList<KStandardItem*> m_children;
KStandardItemModel* m_model;
QHash<QByteArray, QVariant> m_data;
friend class KStandardItemModel;
};

View file

@ -339,9 +339,16 @@ QRectF KStandardItemListWidget::textFocusRect() const
case DetailsLayout: {
QRectF rect = m_textRect;
const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles.first());
const TextInfo* textInfo = m_textInfo.value(m_sortedVisibleRoles.first());
rect.setTop(textInfo->pos.y());
rect.setBottom(textInfo->pos.y() + textInfo->staticText.size().height());
const KItemListStyleOption& option = styleOption();
if (option.extendedSelectionRegion) {
const QString text = textInfo->staticText.text();
rect.setWidth(option.fontMetrics.width(text) + 2 * option.padding);
}
return rect;
}

View file

@ -38,6 +38,8 @@ void KStandardItemModel::insertItem(int index, KStandardItem* item)
m_indexesForItems.insert(item, index);
item->m_model = this;
// TODO: no hierarchical items are handled yet
emit itemsInserted(KItemRangeList() << KItemRange(index, 1));
}
}
@ -77,12 +79,11 @@ int KStandardItemModel::count() const
QHash<QByteArray, QVariant> KStandardItemModel::data(int index) const
{
// TODO: Ugly hack
QHash<QByteArray, QVariant> values;
const KStandardItem* item = m_items[index];
values.insert("text", item->text());
values.insert("iconName", item->icon().name());
return values;
if (item) {
return item->data();
}
return QHash<QByteArray, QVariant>();
}
bool KStandardItemModel::setData(int index, const QHash<QByteArray, QVariant>& values)

View file

@ -173,7 +173,7 @@ void FoldersPanel::slotItemActivated(int index)
{
const KFileItem item = m_model->fileItem(index);
if (!item.isNull()) {
emit changeUrl(item.url(), Qt::LeftButton);
emit folderActivated(item.url());
}
}
@ -181,7 +181,7 @@ void FoldersPanel::slotItemMiddleClicked(int index)
{
const KFileItem item = m_model->fileItem(index);
if (!item.isNull()) {
emit changeUrl(item.url(), Qt::MiddleButton);
emit folderMiddleClicked(item.url());
}
}

View file

@ -51,10 +51,8 @@ public:
void rename(const KFileItem& item);
signals:
/**
* Is emitted if the an URL change is requested.
*/
void changeUrl(const KUrl& url, Qt::MouseButtons buttons);
void folderActivated(const KUrl& url);
void folderMiddleClicked(const KUrl& url);
protected:
/** @see Panel::urlChanged() */

View file

@ -1,6 +1,9 @@
/***************************************************************************
* Copyright (C) 2008-2012 by Peter Penz <peter.penz19@gmail.com> *
* Copyright (C) 2010 by Christian Muehlhaeuser <muesli@gmail.com> *
* *
* Based on KFilePlacesModel from kdelibs: *
* Copyright (C) 2007 Kevin Ottens <ervin@kde.org> *
* Copyright (C) 2007 David Faure <faure@kde.org> *
* *
* 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 *
@ -20,12 +23,18 @@
#include "placespanel.h"
#include <KBookmark>
#include <KBookmarkGroup>
#include <KBookmarkManager>
#include <KComponentData>
#include <KDebug>
#include <KIcon>
#include <kitemviews/kitemlistcontainer.h>
#include <kitemviews/kitemlistcontroller.h>
#include <kitemviews/kstandarditem.h>
#include <kitemviews/kstandarditemlistview.h>
#include <kitemviews/kstandarditemmodel.h>
#include <KStandardDirs>
#include <views/draganddrophelper.h>
#include <QVBoxLayout>
#include <QShowEvent>
@ -33,7 +42,9 @@
PlacesPanel::PlacesPanel(QWidget* parent) :
Panel(parent),
m_controller(0),
m_model(0)
m_model(0),
m_availableDevices(),
m_bookmarkManager(0)
{
}
@ -57,17 +68,17 @@ void PlacesPanel::showEvent(QShowEvent* event)
// Postpone the creating of the controller to the first show event.
// This assures that no performance and memory overhead is given when the folders panel is not
// used at all and stays invisible.
KStandardItemListView* view = new KStandardItemListView();
const QString file = KStandardDirs::locateLocal("data", "kfileplaces/bookmarks.xml");
m_bookmarkManager = KBookmarkManager::managerForFile(file, "kfileplaces");
m_model = new KStandardItemModel(this);
m_model->appendItem(new KStandardItem("Temporary"));
m_model->appendItem(new KStandardItem("out of"));
m_model->appendItem(new KStandardItem("order. Press"));
m_model->appendItem(new KStandardItem("F9 and use"));
m_model->appendItem(new KStandardItem("the left icon"));
m_model->appendItem(new KStandardItem("of the location"));
m_model->appendItem(new KStandardItem("bar instead."));
loadBookmarks();
KStandardItemListView* view = new KStandardItemListView();
m_controller = new KItemListController(m_model, view, this);
m_controller->setSelectionBehavior(KItemListController::SingleSelection);
connect(m_controller, SIGNAL(itemActivated(int)), this, SLOT(slotItemActivated(int)));
connect(m_controller, SIGNAL(itemMiddleClicked(int)), this, SLOT(slotItemMiddleClicked(int)));
KItemListContainer* container = new KItemListContainer(m_controller, this);
container->setEnabledFrame(false);
@ -80,10 +91,57 @@ void PlacesPanel::showEvent(QShowEvent* event)
Panel::showEvent(event);
}
void PlacesPanel::slotItemActivated(int index)
{
const KStandardItem* item = m_model->item(index);
if (item) {
const KUrl url = item->dataValue("url").value<KUrl>();
emit placeActivated(url);
}
}
void PlacesPanel::slotItemMiddleClicked(int index)
{
const KStandardItem* item = m_model->item(index);
if (item) {
const KUrl url = item->dataValue("url").value<KUrl>();
emit placeMiddleClicked(url);
}
}
void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent)
{
Q_UNUSED(parent);
DragAndDropHelper::dropUrls(KFileItem(), dest, event);
}
void PlacesPanel::loadBookmarks()
{
KBookmarkGroup root = m_bookmarkManager->root();
KBookmark bookmark = root.first();
QSet<QString> devices = m_availableDevices;
while (!bookmark.isNull()) {
const QString udi = bookmark.metaDataItem("UDI");
const QString appName = bookmark.metaDataItem("OnlyInApp");
const bool deviceAvailable = devices.remove(udi);
const bool allowedHere = appName.isEmpty() || (appName == KGlobal::mainComponent().componentName());
if ((udi.isEmpty() && allowedHere) || deviceAvailable) {
KStandardItem* item = new KStandardItem();
item->setIcon(KIcon(bookmark.icon()));
item->setText(bookmark.text());
item->setDataValue("address", bookmark.address());
item->setDataValue("url", bookmark.url());
if (deviceAvailable) {
item->setDataValue("udi", udi);
}
m_model->appendItem(item);
}
bookmark = root.next(bookmark);
}
}
#include "placespanel.moc"

View file

@ -22,7 +22,9 @@
#define PLACESPANEL_H
#include <panels/panel.h>
#include <QSet>
class KBookmarkManager;
class KItemListController;
class KStandardItemModel;
@ -37,16 +39,27 @@ public:
PlacesPanel(QWidget* parent);
virtual ~PlacesPanel();
signals:
void placeActivated(const KUrl& url);
void placeMiddleClicked(const KUrl& url);
protected:
virtual bool urlChanged();
virtual void showEvent(QShowEvent* event);
private slots:
void slotItemActivated(int index);
void slotItemMiddleClicked(int index);
void slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent);
private:
void loadBookmarks();
private:
KItemListController* m_controller;
KStandardItemModel* m_model;
QSet<QString> m_availableDevices;
KBookmarkManager* m_bookmarkManager;
};
#endif // PLACESPANEL_H