"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bClipEdit.cpp" (12 Feb 2021, 3939 Bytes) of package /linux/privat/cb2bib-2.0.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "c2bClipEdit.cpp" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.0.0_vs_2.0.1.

    1 /***************************************************************************
    2  *   Copyright (C) 2004-2021 by Pere Constans
    3  *   constans@molspaces.com
    4  *   cb2Bib version 2.0.1. Licensed under the GNU GPL version 3.
    5  *   See the LICENSE file that comes with this distribution.
    6  ***************************************************************************/
    7 #include "c2bClipEdit.h"
    8 
    9 #include "c2b.h"
   10 #include "c2bBibHighlighter.h"
   11 #include "c2bHighlighter.h"
   12 
   13 #include <QAction>
   14 #include <QContextMenuEvent>
   15 #include <QMenu>
   16 #include <QScrollBar>
   17 #include <QTextCursor>
   18 
   19 
   20 c2bClipEdit::c2bClipEdit(const Mode mode, QWidget* parentw) : QPlainTextEdit(parentw)
   21 {
   22     setFrameShape(QPlainTextEdit::StyledPanel);
   23     setFrameShadow(QPlainTextEdit::Raised);
   24     viewport()->setAcceptDrops(false);
   25     _has_selecting_ended = false;
   26     _is_tagged_view = false;
   27     if (mode == Editor)
   28     {
   29         new c2bHighlighter(document());
   30         setReadOnly(false);
   31         connect(this, SIGNAL(selectionChanged()), this, SLOT(selectionHasChanged()));
   32     }
   33     else if (mode == Browser)
   34     {
   35         new c2bBibHighlighter(document());
   36         setReadOnly(true);
   37     }
   38 }
   39 
   40 c2bClipEdit::~c2bClipEdit() {}
   41 
   42 
   43 void c2bClipEdit::setText(const QString& text, const QString& tagged_text)
   44 {
   45     _text = text;
   46     if (tagged_text.isEmpty())
   47         _tagged_text = text;
   48     else
   49         _tagged_text = tagged_text;
   50     if (_is_tagged_view)
   51         setPlainText(_tagged_text);
   52     else
   53         setPlainText(_text);
   54 }
   55 
   56 void c2bClipEdit::insertTag(const QString& tag)
   57 {
   58     if (_is_tagged_view)
   59         insertPlainText(tag);
   60     else
   61         insertPlainText("[[" + textCursor().selectedText() + "]]");
   62 }
   63 
   64 void c2bClipEdit::setTaggedViewMode(bool tagged_view)
   65 {
   66     _is_tagged_view = tagged_view;
   67     if (_is_tagged_view)
   68     {
   69         _text = toPlainText();
   70         setPlainText(_tagged_text);
   71     }
   72     else
   73     {
   74         _tagged_text = toPlainText();
   75         setPlainText(_text);
   76     }
   77 }
   78 
   79 void c2bClipEdit::updatePlainText(const QString& text)
   80 {
   81     const int scrollPosition(verticalScrollBar()->value());
   82     setPlainText(text);
   83     verticalScrollBar()->setValue(scrollPosition);
   84 }
   85 
   86 void c2bClipEdit::keyPressEvent(QKeyEvent* qevent)
   87 {
   88     _has_selecting_ended = false;
   89     QPlainTextEdit::keyPressEvent(qevent);
   90 }
   91 
   92 void c2bClipEdit::keyReleaseEvent(QKeyEvent* qevent)
   93 {
   94     QPlainTextEdit::keyReleaseEvent(qevent);
   95     if (_has_selecting_ended && qevent->key() == Qt::Key_Shift)
   96         if (textCursor().hasSelection())
   97         {
   98             _has_selecting_ended = false;
   99             emit userEndedSelection(mapToGlobal(cursorRect().topLeft()));
  100         }
  101 }
  102 
  103 void c2bClipEdit::mouseReleaseEvent(QMouseEvent* qevent)
  104 {
  105     QPlainTextEdit::mouseReleaseEvent(qevent);
  106     if (qevent->button() == Qt::LeftButton)
  107         if (textCursor().hasSelection())
  108             emit userEndedSelection(QCursor::pos());
  109     _has_selecting_ended = false;
  110 }
  111 
  112 void c2bClipEdit::selectionHasChanged()
  113 {
  114     _has_selecting_ended = textCursor().hasSelection();
  115 }
  116 
  117 void c2bClipEdit::contextMenuEvent(QContextMenuEvent* qevent)
  118 {
  119     QMenu* edit_menu = createStandardContextMenu();
  120     edit_menu->setTitle(tr("Edit..."));
  121     QList<QAction*> acts = edit_menu->actions();
  122     if (!isReadOnly())
  123     {
  124         QAction* act;
  125         act = new QAction(tr("Clear"), edit_menu);
  126         act->setEnabled(acts.last()->isEnabled()); // As is Select All
  127         connect(act, SIGNAL(triggered()), this, SLOT(clear()));
  128         edit_menu->insertAction(acts.at(7), act);
  129     }
  130     QMenu* menu = new QMenu(this);
  131     menu->addMenu(edit_menu);
  132     menu->addSeparator();
  133     acts = c2b::netqinf()->bookmarkActions(textCursor().selectedText());
  134     for (int i = 0; i < acts.count(); ++i)
  135         menu->addAction(acts.at(i));
  136     acts = actions();
  137     for (int i = 0; i < acts.count(); ++i)
  138         menu->addAction(acts.at(i));
  139     menu->exec(qevent->globalPos());
  140     delete edit_menu;
  141     delete menu;
  142 }