Skip to content

Commit

Permalink
Editor context menu: added a close Action to clear the view
Browse files Browse the repository at this point in the history
  • Loading branch information
SZinedine committed Sep 21, 2020
1 parent fd4a86a commit 73c2feb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/editorwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ EditorWidget::EditorWidget(QWidget* parent)
layout->addWidget(m_editor);
layout->setContentsMargins(0, 0, 0, 0);
// m_toolBar->addAction("save", [&] { save(); });
m_editor->setReadOnly(true);
m_watcher = new QFileSystemWatcher(this);

// connect(this, &EditorWidget::openedFile, this, [=] { setVisible(true); });
// connect(this, &EditorWidget::closedFile, this, [=] { setVisible(false); });
connect(m_watcher, &QFileSystemWatcher::fileChanged, this, [=] { reload(); });
connect(m_editor, &MarkdownEditorWidget::toClose, this, &EditorWidget::closeFile);
}


Expand Down
51 changes: 49 additions & 2 deletions src/markdowneditorwidget.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,52 @@
/*************************************************************************
* DeepTags, Markdown Notes Manager
* Copyright (C) 2020 Zineddine Saibi
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
#include "markdowneditorwidget.h"
#include <QContextMenuEvent>
#include <QMenu>

MarkdownEditorWidget::MarkdownEditorWidget(QWidget* parent)
: QMarkdownTextEdit(parent)
{}
: QMarkdownTextEdit(parent), m_occupied(false)
{
setReadOnly(true);
}

void MarkdownEditorWidget::contextMenuEvent(QContextMenuEvent *event) {
QMenu *menu = createStandardContextMenu();
QAction* closeFileAction = new QAction(tr("Close File"), this);
menu->addSeparator();
menu->addAction(closeFileAction);
closeFileAction->setEnabled(m_occupied);

connect(closeFileAction, &QAction::triggered, [this]{ emit this->toClose(); });
menu->exec(event->globalPos());

delete menu;
delete closeFileAction;
}

void MarkdownEditorWidget::clear() {
QPlainTextEdit::clear();
setOccupied(false);
}

void MarkdownEditorWidget::setText(const QString& text) {
QMarkdownTextEdit::setText(text);
setOccupied(true);
}


31 changes: 30 additions & 1 deletion src/markdowneditorwidget.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@
/*************************************************************************
* DeepTags, Markdown Notes Manager
* Copyright (C) 2020 Zineddine Saibi
*
* 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
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*************************************************************************/
#include <qmarkdowntextedit.h>

class MarkdownEditorWidget : public QMarkdownTextEdit
{
Q_OBJECT
public:
explicit MarkdownEditorWidget(QWidget* parent=nullptr);

~MarkdownEditorWidget() {};
void contextMenuEvent(QContextMenuEvent *event) override;
inline void setOccupied(bool o) { m_occupied = o; }
inline bool occupied() const { return m_occupied; }
void clear();
void setText(const QString& text);

signals:
void toClose();

private:
/* is there a file displayed or not */
bool m_occupied;
};

0 comments on commit 73c2feb

Please sign in to comment.