From b207950cc771763a2a6788723129bfcaff274cb6 Mon Sep 17 00:00:00 2001 From: Martijn Vogelaar Date: Wed, 12 Aug 2020 09:58:30 +0000 Subject: [PATCH] #15 Drag drop layout text --- .../controllers/layoutscontroller.cpp | 24 +++++++++++++++++++ app/settings/controllers/layoutscontroller.h | 1 + app/settings/handlers/tablayoutshandler.cpp | 19 +++++++++++++-- app/settings/handlers/tablayoutshandler.h | 1 + app/settings/views/layoutstableview.cpp | 10 +++++++- app/settings/views/layoutstableview.h | 3 ++- 6 files changed, 54 insertions(+), 4 deletions(-) diff --git a/app/settings/controllers/layoutscontroller.cpp b/app/settings/controllers/layoutscontroller.cpp index 55bbdb10b..4bf6a939a 100644 --- a/app/settings/controllers/layoutscontroller.cpp +++ b/app/settings/controllers/layoutscontroller.cpp @@ -46,6 +46,7 @@ #include #include #include +#include // KDE #include @@ -556,6 +557,29 @@ const Latte::Data::Layout Layouts::addLayoutForFile(QString file, QString layout return copied; } +const Latte::Data::Layout Layouts::addLayoutByText(QString rawLayoutText) +{ + QTemporaryFile tempFile; + tempFile.open(); + QTextStream stream(&tempFile); + stream << rawLayoutText; + stream.flush(); + tempFile.close(); + + Latte::Data::Layout newLayout = addLayoutForFile(tempFile.fileName(),i18n("Dropped Raw Layout")); + + int selectedRow = m_view->currentIndex().row(); + QModelIndex tIndex = m_proxyModel->index(selectedRow, Model::Layouts::NAMECOLUMN); + m_view->edit(tIndex); + + /**Window has to be activated explicitely since the window where the drag + * started would otherwise be the active window. By activating the window + the user can immediately change the name by simply typing.*/ + m_handler->dialog()->activateWindow(); + + return newLayout; +} + void Layouts::copySelectedLayout() { int row = m_view->currentIndex().row(); diff --git a/app/settings/controllers/layoutscontroller.h b/app/settings/controllers/layoutscontroller.h index 2f40d8052..3860c3d9b 100644 --- a/app/settings/controllers/layoutscontroller.h +++ b/app/settings/controllers/layoutscontroller.h @@ -93,6 +93,7 @@ public: void copySelectedLayout(); const Latte::Data::Layout addLayoutForFile(QString file, QString layoutName = QString(), bool newTempDirectory = true); + const Latte::Data::Layout addLayoutByText(QString rawLayoutText); //! import layouts from Latte versions <= v0.7.x bool importLayoutsFromV1ConfigFile(QString file); diff --git a/app/settings/handlers/tablayoutshandler.cpp b/app/settings/handlers/tablayoutshandler.cpp index 342d53d19..224192d65 100644 --- a/app/settings/handlers/tablayoutshandler.cpp +++ b/app/settings/handlers/tablayoutshandler.cpp @@ -685,6 +685,13 @@ void TabLayouts::on_layoutFilesDropped(const QStringList &paths) } } +void TabLayouts::on_rawLayoutDropped(const QString &rawLayout) +{ + Latte::Data::Layout importedlayout = m_layoutsController->addLayoutByText(rawLayout); + showInlineMessage(i18nc("settings:layout imported successfully","Layout %0 imported successfully...").arg(importedlayout.name), + KMessageWidget::Information); +} + bool TabLayouts::isCurrentTab() const { return (m_layoutMenu->isEnabled() && (m_parentDialog->currentPage() == Dialog::LayoutPage)); @@ -721,7 +728,7 @@ void TabLayouts::on_dragEnterEvent(QDragEnterEvent *event) } event->acceptProposedAction(); - m_ui->layoutsView->dragEntered(); + m_ui->layoutsView->dragEntered(event); } void TabLayouts::on_dragLeaveEvent(QDragLeaveEvent *event) @@ -738,7 +745,6 @@ void TabLayouts::on_dragMoveEvent(QDragMoveEvent *event) } event->acceptProposedAction(); - m_ui->layoutsView->dragEntered(); } void TabLayouts::on_dropEvent(QDropEvent *event) @@ -766,6 +772,15 @@ void TabLayouts::on_dropEvent(QDropEvent *event) on_layoutFilesDropped(paths); } + m_ui->layoutsView->dragLeft(); + } else if (event->mimeData()->hasText()){ + if(!event->mimeData()->text().isEmpty()){ + on_rawLayoutDropped(event->mimeData()->text()); + } else if(!event->mimeData()->data("text/plain").isEmpty()) { + on_rawLayoutDropped(event->mimeData()->data("text/plain")); + } else { + qDebug() << "Data from drag could not be retrieved!"; + } m_ui->layoutsView->dragLeft(); } } diff --git a/app/settings/handlers/tablayoutshandler.h b/app/settings/handlers/tablayoutshandler.h index 491cb4155..c989cdb71 100644 --- a/app/settings/handlers/tablayoutshandler.h +++ b/app/settings/handlers/tablayoutshandler.h @@ -104,6 +104,7 @@ private slots: void on_currentPageChanged(int page); void on_layoutFilesDropped(const QStringList &paths); + void on_rawLayoutDropped(const QString &rawLayout); void updatePerLayoutButtonsState(); private: diff --git a/app/settings/views/layoutstableview.cpp b/app/settings/views/layoutstableview.cpp index 9505787c1..56be68282 100644 --- a/app/settings/views/layoutstableview.cpp +++ b/app/settings/views/layoutstableview.cpp @@ -21,6 +21,7 @@ //! Qt #include +#include //! KDE #include @@ -70,12 +71,19 @@ void LayoutsTableView::paintEvent(QPaintEvent *event) QTableView::paintEvent(event); } -void LayoutsTableView::dragEntered() +void LayoutsTableView::dragEntered(QDragEnterEvent *event) { m_overlayDropMessage->move(MARGIN, MARGIN); m_overlayDropMessage->resize(width() - 2*MARGIN, height() - 2*MARGIN); m_overlayDropMessage->raise(); + if (event->mimeData()->hasUrls()) { + m_overlayDropMessage->setText(i18n("Drop layout files here...")); + } else if(event->mimeData()->hasText()) { + m_overlayDropMessage->setText(i18n("Drop raw layout text here...")); + } else { + m_overlayDropMessage->setText(i18n("Unsupported data!")); + } m_overlayDropMessage->setVisible(true); } diff --git a/app/settings/views/layoutstableview.h b/app/settings/views/layoutstableview.h index 70930e7b7..524f8db3a 100644 --- a/app/settings/views/layoutstableview.h +++ b/app/settings/views/layoutstableview.h @@ -24,6 +24,7 @@ #include #include #include +#include namespace Latte { namespace Settings { @@ -35,7 +36,7 @@ class LayoutsTableView : public QTableView public: LayoutsTableView(QWidget *parent = nullptr); - void dragEntered(); + void dragEntered(QDragEnterEvent *event); void dragLeft(); protected: