WSL/SLF GitLab Repository

Commit 9869ccac authored by Michael Reisecker's avatar Michael Reisecker
Browse files

Implement file dropping into the Preview Editor.

The drag & drop behaviour of the text editor itself is kept as-is
(moving text blocks with the mouse etc., and also inserting file names
if dropped into the text area) because we don't want to re-implement all
of this ourselves.
However, we display a guidance text if a user drags files over the text
area telling them where to drop them to open.
parent b213da22
......@@ -42,13 +42,15 @@
#include "PreviewEdit.h"
#include "src/main/colors.h"
#include <QtGui>
#include <QMainWindow>
#include <QtWidgets>
PreviewEdit::PreviewEdit(const bool& monospace) : QPlainTextEdit()
{
if (monospace) setMonospacedFont();
sidePanel = new PreviewSidePanel(this);
sidePanel->setStyleSheet("QWidget {background-color: " + colors::getQColor("syntax_background").name() + "; color: " + colors::getQColor("sl_base01").name() + "; font-style: italic; font-size: 9pt}");
sidePanel->setStyleSheet("QWidget {background-color: " + colors::getQColor("syntax_background").name() + "; color: " +
colors::getQColor("sl_base01").name() + "; font-style: italic; font-size: 9pt}");
connect(this, &PreviewEdit::blockCountChanged, this, &PreviewEdit::updateSidePanelWidth);
connect(this, &PreviewEdit::updateRequest, this, &PreviewEdit::updateSidePanel);
......@@ -92,13 +94,35 @@ void PreviewEdit::updateSidePanel(const QRect &rect, int dy)
updateSidePanelWidth();
}
void PreviewEdit::resizeEvent(QResizeEvent *e)
void PreviewEdit::resizeEvent(QResizeEvent *event)
{
QPlainTextEdit::resizeEvent(e);
QPlainTextEdit::resizeEvent(event);
const QRect cr( contentsRect() );
sidePanel->setGeometry(QRect(cr.left(), cr.top(), getSidePanelWidth(), cr.height()));
}
/**
* @brief Show info when users drag files over the editor.
* @details We let the text editor handle the drag and drop behaviour. However, we give notice that
* dropping files in an area outside of the text editor itself will open the files.
* @param[in] event The drag move event.
*/
void PreviewEdit::dragMoveEvent(QDragMoveEvent *event)
{
QMainWindow* preview_window(qobject_cast<QMainWindow*>(this->parent()->parent()->parent())); //QStackedWidget->QTabWidget->PreviewWindow
if (preview_window) {
if (event->mimeData()->hasUrls()) {
preview_window->statusBar()->showMessage(tr("Drop files over the menu or tab titles to open."));
preview_window->statusBar()->show(); //might be hidden from closing search bar
}
}
event->acceptProposedAction(); //progress as normal
//Note: overriding QDragENTERevent instead causes "QDragManager::drag in possibly invalid state" for no apparent reason,
//but this here should be good.
}
void PreviewEdit::repaintSidePanel(QPaintEvent *event)
{
QPainter painter(sidePanel);
......
......@@ -57,7 +57,8 @@ class PreviewEdit : public QPlainTextEdit
protected:
void setMonospacedFont();
void resizeEvent(QResizeEvent *event);
void resizeEvent(QResizeEvent *event) override;
void dragMoveEvent(QDragMoveEvent *event) override;
private slots:
void updateSidePanelWidth();
......@@ -75,12 +76,14 @@ class PreviewSidePanel : public QWidget
public:
PreviewSidePanel(PreviewEdit *editor) : QWidget(editor), textEdit(editor) {}
QSize sizeHint() const {
QSize sizeHint() const
{
return QSize(textEdit->getSidePanelWidth(), 0);
}
protected:
void paintEvent(QPaintEvent *event) {
void paintEvent(QPaintEvent *event)
{
textEdit->repaintSidePanel(event);
}
......
......@@ -193,7 +193,8 @@ PreviewWindow::PreviewWindow(QMainWindow *parent) : QMainWindow(parent),
connect(file_tabs_, &QTabWidget::tabCloseRequested, this, &PreviewWindow::closeTab);
file_tabs_->setTabsClosable(true);
this->setCentralWidget(file_tabs_);
createMenu();
createMenu();
setAcceptDrops(true);
//size this window (from "outside" via a timer to have it work on macOS; works without for the logger?):
QTimer::singleShot(1, [=]{ dim::setDimensions(this, dim::PREVIEW); });
......@@ -242,7 +243,9 @@ void PreviewWindow::addIniTab(const QString& infile)
this->statusBar()->hide();
}
preview_editor->setPlainText(ini_contents); //without undo history --> can't undo to empty
QFileInfo file_info(getMainWindow()->getIni()->getFilename());
const QString loaded_file = infile.isNull()? getMainWindow()->getIni()->getFilename() : infile;
QFileInfo file_info(loaded_file);
QString file_name(file_info.fileName());
QString file_path;
if (file_info.exists()) //avoid warning
......@@ -309,6 +312,31 @@ void PreviewWindow::keyPressEvent(QKeyEvent *event)
}
}
/**
* @brief Event listener for drag events.
* @details This function reacts if a user starts dragging content into the Preview Editor
* and accepts the dragging.
* @param[in] event The drag event.
*/
void PreviewWindow::dragEnterEvent(QDragEnterEvent *event)
{
event->acceptProposedAction();
}
/**
* @brief Event listener for drop events.
* @details This is an event listener for the parent preview window, not the text editor itself.
* As such, the text editor will sill handle its drag & drop functionality accordingly. In order
* for users to be able to drop files, they need to drop it ouside of the text area. When dragging
* into the text area however, an info status about this is displayed.
* @param[in] event The drop event.
*/
void PreviewWindow::dropEvent(QDropEvent *event)
{
for (const QUrl &url : event->mimeData()->urls()) //run through all files
addIniTab(url.toLocalFile());
}
/**
* @brief Close an INI file's tab.
* @param[in] index Index of the tab that is being closed.
......@@ -1121,7 +1149,7 @@ QString PreviewWindow::trimComment(const QString &comment) const
}
/**
* @brief Menu item to open an existing INI file in the Preview Editor.
* @brief Menu item to open an existing INI file in the Preview Editor.
*/
void PreviewWindow::openFile()
{
......
......@@ -82,6 +82,8 @@ class PreviewWindow : public QMainWindow
protected:
void closeEvent(QCloseEvent* event) override;
void keyPressEvent(QKeyEvent *event) override;
void dragEnterEvent(QDragEnterEvent *event) override;
void dropEvent(QDropEvent *event) override;
private:
enum insert_text { //options for the insert menu
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment