Skip to content

Commit

Permalink
Move preview context menu functionality to NotePreviewWidget for #800
Browse files Browse the repository at this point in the history
  • Loading branch information
pbek committed Jun 22, 2019
1 parent 376ed98 commit 390475b
Show file tree
Hide file tree
Showing 42 changed files with 6,688 additions and 6,430 deletions.
351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ar.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_bn.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ca.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ceb.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_cs.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_da.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_de.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_el.ts

Large diffs are not rendered by default.

353 changes: 178 additions & 175 deletions src/languages/QOwnNotes_en.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_es.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_fi.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_fil.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_fr.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_gl.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_hi.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_hil.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_hr.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_hu.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_id.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_it.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ja.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_nl.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_no.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_pl.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_pt_BR.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_pt_PT.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ro.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ru.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_sk.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_sl.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_sv.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_tl.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_tr.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_uk.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_ur.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_zh_CN.ts

Large diffs are not rendered by default.

351 changes: 179 additions & 172 deletions src/languages/QOwnNotes_zh_TW.ts

Large diffs are not rendered by default.

54 changes: 0 additions & 54 deletions src/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10400,60 +10400,6 @@ void MainWindow::updateNoteSortOrderSelectorVisibility(bool visible) {
// ui->sortOrderSeparator->setVisible(visible);
}

/**
* Shows a context menu for the note preview
*
* @param pos
*/
void MainWindow::on_noteTextView_customContextMenuRequested(const QPoint &pos) {
QPoint globalPos = ui->noteTextView->mapToGlobal(pos);
QMenu *menu = ui->noteTextView->createStandardContextMenu();

QTextCursor c = ui->noteTextView->cursorForPosition(pos);
QTextFormat format = c.charFormat();
const QString &anchorHref = format.toCharFormat().anchorHref();
bool isImageFormat = format.isImageFormat();
bool isAnchor = !anchorHref.isEmpty();

if (isImageFormat || isAnchor) {
menu->addSeparator();
}

auto *copyImageAction = new QAction(this);
auto *copyLinkLocationAction = new QAction(this);

// check if clicked object was an image
if (isImageFormat) {
copyImageAction = menu->addAction(tr("Copy image file path"));
}

if (isAnchor) {
copyLinkLocationAction = menu->addAction(tr("Copy link location"));
}

QAction *selectedItem = menu->exec(globalPos);

if (selectedItem) {
// copy the image file path to the clipboard
if (selectedItem == copyImageAction) {
QString imagePath = format.toImageFormat().name();
QUrl imageUrl = QUrl(imagePath);

if (imageUrl.isLocalFile()) {
imagePath = imageUrl.toLocalFile();
}

QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(imagePath);
}
// copy link location to the clipboard
else if (selectedItem == copyLinkLocationAction) {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(anchorHref);
}
}
}

/**
* Commits changes from the current note folder to git
*/
Expand Down
2 changes: 0 additions & 2 deletions src/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -503,8 +503,6 @@ private slots:

bool restoreActiveNoteHistoryItem();

void on_noteTextView_customContextMenuRequested(const QPoint &pos);

void on_actionShow_note_git_versions_triggered();

void on_tagTreeWidget_itemCollapsed(QTreeWidgetItem *item);
Expand Down
5 changes: 1 addition & 4 deletions src/mainwindow.ui
Original file line number Diff line number Diff line change
Expand Up @@ -792,9 +792,6 @@
<property name="enabled">
<bool>true</bool>
</property>
<property name="contextMenuPolicy">
<enum>Qt::CustomContextMenu</enum>
</property>
<property name="styleSheet">
<string notr="true"/>
</property>
Expand Down Expand Up @@ -2499,9 +2496,9 @@ p, li { white-space: pre-wrap; }
</customwidget>
</customwidgets>
<resources>
<include location="breeze-qownnotes.qrc"/>
<include location="images.qrc"/>
<include location="breeze-dark-qownnotes.qrc"/>
<include location="breeze-qownnotes.qrc"/>
</resources>
<connections/>
</ui>
61 changes: 60 additions & 1 deletion src/widgets/notepreviewwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
#include <QDebug>
#include <QRegExp>
#include <QMovie>
#include <QAction>
#include <QApplication>
#include <QMenu>
#include <QClipboard>
#include <QProxyStyle>

class NoDottedOutlineForLinksStyle: public QProxyStyle {
Expand All @@ -37,7 +41,7 @@ NotePreviewWidget::NotePreviewWidget(QWidget *parent) : QTextBrowser(parent) {
_searchWidget->setReplaceEnabled(false);

// add a layout to the widget
QVBoxLayout *layout = new QVBoxLayout;
auto *layout = new QVBoxLayout;
layout->setContentsMargins(0, 0, 0, 0);
layout->setMargin(0);
layout->addStretch();
Expand Down Expand Up @@ -203,3 +207,58 @@ void NotePreviewWidget::hide() {
_searchWidget->hide();
QWidget::hide();
}

/**
* Shows a context menu for the note preview
*
* @param event
*/
void NotePreviewWidget::contextMenuEvent(QContextMenuEvent *event) {
QPoint pos = event->pos();
QPoint globalPos = event->globalPos();
QMenu *menu = this->createStandardContextMenu();

QTextCursor c = this->cursorForPosition(pos);
QTextFormat format = c.charFormat();
const QString &anchorHref = format.toCharFormat().anchorHref();
bool isImageFormat = format.isImageFormat();
bool isAnchor = !anchorHref.isEmpty();

if (isImageFormat || isAnchor) {
menu->addSeparator();
}

auto *copyImageAction = new QAction(this);
auto *copyLinkLocationAction = new QAction(this);

// check if clicked object was an image
if (isImageFormat) {
copyImageAction = menu->addAction(tr("Copy image file path"));
}

if (isAnchor) {
copyLinkLocationAction = menu->addAction(tr("Copy link location"));
}

QAction *selectedItem = menu->exec(globalPos);

if (selectedItem) {
// copy the image file path to the clipboard
if (selectedItem == copyImageAction) {
QString imagePath = format.toImageFormat().name();
QUrl imageUrl = QUrl(imagePath);

if (imageUrl.isLocalFile()) {
imagePath = imageUrl.toLocalFile();
}

QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(imagePath);
}
// copy link location to the clipboard
else if (selectedItem == copyLinkLocationAction) {
QClipboard *clipboard = QApplication::clipboard();
clipboard->setText(anchorHref);
}
}
}
7 changes: 5 additions & 2 deletions src/widgets/notepreviewwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <QTextBrowser>
#include <QResizeEvent>
#include <QPoint>
#include <widgets/qtexteditsearchwidget.h>

class NotePreviewWidget : public QTextBrowser
Expand All @@ -32,12 +33,14 @@ class NotePreviewWidget : public QTextBrowser
QTextEditSearchWidget *_searchWidget;
QWidget *_searchFrame;

void resizeEvent(QResizeEvent* event);
bool eventFilter(QObject *obj, QEvent *event);
void resizeEvent(QResizeEvent* event) override;
bool eventFilter(QObject *obj, QEvent *event) override;

QStringList extractGifUrls(const QString &text) const;
void animateGif(const QString &text);

void contextMenuEvent(QContextMenuEvent *event) override;

public slots:
void hide();

Expand Down

0 comments on commit 390475b

Please sign in to comment.