1
0
mirror of https://github.com/KDE/latte-dock.git synced 2024-12-23 13:33:50 +03:00

#15 Drag drop layout text

This commit is contained in:
Martijn Vogelaar 2020-08-12 09:58:30 +00:00 committed by Michail Vourlakos
parent 3ea099a54b
commit b207950cc7
6 changed files with 54 additions and 4 deletions

View File

@ -46,6 +46,7 @@
#include <QItemSelection>
#include <QStringList>
#include <QTemporaryDir>
#include <QTemporaryFile>
// KDE
#include <KArchive/KTar>
@ -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();

View File

@ -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);

View File

@ -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 <b>%0</b> 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();
}
}

View File

@ -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:

View File

@ -21,6 +21,7 @@
//! Qt
#include <QDebug>
#include <QMimeData>
//! KDE
#include <KLocalizedString>
@ -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);
}

View File

@ -24,6 +24,7 @@
#include <QLabel>
#include <QPaintEvent>
#include <QTableView>
#include <QDragEnterEvent>
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: