From b419cfc5974192f3445b6a439b40bae4af119615 Mon Sep 17 00:00:00 2001 From: Peter Penz Date: Sat, 28 Apr 2012 23:58:56 +0200 Subject: Places Panel fixes Implement adding, editing and removing of entries. Note that the result currently is still not stored in bookmarks.xml (this needs to wait until the hiding is implemented in the model). --- src/panels/places/placespanel.cpp | 97 +++++++++++++++++++++++++++++++-------- 1 file changed, 77 insertions(+), 20 deletions(-) (limited to 'src/panels/places/placespanel.cpp') diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index d42b75f5a..bcdccf2a0 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -24,6 +24,7 @@ #include "placespanel.h" #include +#include #include #include #include @@ -31,6 +32,8 @@ #include #include #include +#include +#include #include #include #include @@ -89,6 +92,8 @@ void PlacesPanel::showEvent(QShowEvent* event) QVBoxLayout* layout = new QVBoxLayout(this); layout->setMargin(0); layout->addWidget(container); + + selectClosestItem(); } Panel::showEvent(event); @@ -124,6 +129,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) QAction* tearDownAction = 0; QAction* ejectAction = 0; + const bool isSystemItem = m_model->isSystemItem(index); const bool isDevice = !data.value("udi").toString().isEmpty(); if (isDevice) { ejectAction = m_model->ejectAction(index); @@ -149,8 +155,10 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) menu.addSeparator(); } addAction = menu.addAction(KIcon("document-new"), i18nc("@item:inmenu", "Add Entry...")); - mainSeparator = menu.addSeparator(); - editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit Entry '%1'...", label)); + if (!isSystemItem) { + mainSeparator = menu.addSeparator(); + editAction = menu.addAction(KIcon("document-properties"), i18nc("@item:inmenu", "Edit Entry '%1'...", label)); + } } if (!addAction) { @@ -172,7 +180,7 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) } QAction* removeAction = 0; - if (!isDevice) { + if (!isDevice && !isSystemItem) { removeAction = menu.addAction(KIcon("edit-delete"), i18nc("@item:inmenu", "Remove Entry '%1'", label)); } @@ -182,22 +190,23 @@ void PlacesPanel::slotItemContextMenuRequested(int index, const QPointF& pos) } QAction* action = menu.exec(pos.toPoint()); - if (!action) { - return; + if (action) { + if (action == emptyTrashAction) { + emptyTrash(); + } else if (action == addAction) { + addEntry(); + } else if (action == editAction) { + editEntry(index); + } else if (action == removeAction) { + m_model->removeItem(index); + } else if (action == hideAction) { + } else if (action == showAllAction) { + } else if (action == tearDownAction) { + } else if (action == ejectAction) { + } } - if (action == emptyTrashAction) { - emptyTrash(); - } else if (action == addAction) { - addEntry(); - } else if (action == editAction) { - editEntry(index); - } else if (action == removeAction) { - } else if (action == hideAction) { - } else if (action == showAllAction) { - } else if (action == tearDownAction) { - } else if (action == ejectAction) { - } + selectClosestItem(); } void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos) @@ -214,6 +223,8 @@ void PlacesPanel::slotViewContextMenuRequested(const QPointF& pos) if (action == addAction) { addEntry(); } + + selectClosestItem(); } void PlacesPanel::slotUrlsDropped(const KUrl& dest, QDropEvent* event, QWidget* parent) @@ -252,11 +263,30 @@ void PlacesPanel::emptyTrash() void PlacesPanel::addEntry() { + const int index = m_controller->selectionManager()->currentItem(); + const KUrl url = m_model->data(index).value("url").value(); + QPointer dialog = new PlacesItemEditDialog(this); dialog->setCaption(i18nc("@title:window", "Add Places Entry")); dialog->setAllowGlobal(true); + dialog->setUrl(url); if (dialog->exec() == QDialog::Accepted) { - // TODO + KStandardItem* item = createStandardItemFromDialog(dialog); + + // Insert the item as last item of the "Places" group + bool inserted = false; + int i = 0; + while (!inserted && i < m_model->count()) { + if (m_model->item(i)->group() != m_model->placesGroupName()) { + m_model->insertItem(i, item); + inserted = true; + } + ++i; + } + + if (!inserted) { + m_model->appendItem(item); + } } delete dialog; @@ -264,7 +294,7 @@ void PlacesPanel::addEntry() void PlacesPanel::editEntry(int index) { - const QHash data = m_model->data(index); + QHash data = m_model->data(index); QPointer dialog = new PlacesItemEditDialog(this); dialog->setCaption(i18nc("@title:window", "Edit Places Entry")); @@ -273,10 +303,37 @@ void PlacesPanel::editEntry(int index) dialog->setUrl(data.value("url").value()); dialog->setAllowGlobal(true); if (dialog->exec() == QDialog::Accepted) { - // TODO + KStandardItem* oldItem = m_model->item(index); + if (oldItem) { + KStandardItem* item = createStandardItemFromDialog(dialog); + item->setGroup(oldItem->group()); + m_model->replaceItem(index, item); + } } delete dialog; } +void PlacesPanel::selectClosestItem() +{ + const int index = m_model->closestItem(url()); + KItemListSelectionManager* selectionManager = m_controller->selectionManager(); + selectionManager->setCurrentItem(index); + selectionManager->clearSelection(); + selectionManager->setSelected(index); +} + +KStandardItem* PlacesPanel::createStandardItemFromDialog(PlacesItemEditDialog* dialog) const +{ + Q_ASSERT(dialog); + + KStandardItem* item = new KStandardItem(); + item->setIcon(KIcon(dialog->icon())); + item->setText(dialog->text()); + item->setDataValue("url", dialog->url()); + item->setGroup(m_model->placesGroupName()); + + return item; +} + #include "placespanel.moc" -- cgit v1.3