"Fossies" - the Fresh Open Source Software Archive

Member "cb2bib-2.0.1/src/c2bTextEdit.cpp" (12 Feb 2021, 9804 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 "c2bTextEdit.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 "c2bTextEdit.h"
    8 
    9 #include "c2bSettings.h"
   10 #include "c2bUtils.h"
   11 
   12 #include <QMenu>
   13 #include <QPainter>
   14 #include <QScrollBar>
   15 #include <QTextBlock>
   16 
   17 
   18 c2bTextEdit::c2bTextEdit(QWidget* parentw) : QPlainTextEdit(parentw)
   19 {
   20     setFrameShape(QPlainTextEdit::StyledPanel);
   21     setFrameShadow(QPlainTextEdit::Raised);
   22     _settingsP = c2bSettingsP;
   23     connect(_settingsP, SIGNAL(newSettings()), this, SLOT(loadSettings()));
   24 
   25     _line_number_area = new LineNumberArea(this);
   26     _line_number_area_color = palette().color(QPalette::Active, QPalette::Base).darker(110);
   27     _line_number_color_p = &_settingsP->colors.comment_color;
   28     connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(updateLineNumberAreaWidth()));
   29     connect(this, SIGNAL(updateRequest(QRect,int)), this, SLOT(updateLineNumberArea(QRect,int)));
   30     updateLineNumberAreaWidth();
   31 
   32     c2bUtils::addSeparator(this);
   33     toggleWordWrappingAction = new QAction(this);
   34     addAction(toggleWordWrappingAction);
   35     toggleWordWrappingAction->setText(tr("Toggle Word Wrapping"));
   36     toggleWordWrappingAction->setStatusTip(tr("Toggle word wrapping"));
   37     _settingsP->setKeysequence("Shortcuts/Editor/", toggleWordWrappingAction);
   38     connect(toggleWordWrappingAction, SIGNAL(triggered()), this, SLOT(toggleWordWrapping()));
   39     if (_settingsP->value("c2bTextEdit/WrapMode", false).toBool())
   40         setLineWrapMode(QPlainTextEdit::WidgetWidth);
   41     else
   42         setLineWrapMode(QPlainTextEdit::NoWrap);
   43 }
   44 
   45 c2bTextEdit::~c2bTextEdit()
   46 {
   47     _settingsP->setValue("c2bTextEdit/WrapMode", lineWrapMode() == QPlainTextEdit::WidgetWidth);
   48 }
   49 
   50 
   51 void c2bTextEdit::setBibTeXActions()
   52 {
   53     c2bUtils::addSeparator(this);
   54     pasteCurrentBibTeXAction = new QAction(this);
   55     addAction(pasteCurrentBibTeXAction);
   56     pasteCurrentBibTeXAction->setText(tr("Paste Current BibTeX"));
   57     pasteCurrentBibTeXAction->setStatusTip(tr("Paste current BibTeX"));
   58     _settingsP->setKeysequence("Shortcuts/Editor/", pasteCurrentBibTeXAction);
   59 
   60     setLaTeXActions();
   61 
   62     c2bUtils::addSeparator(this);
   63     journalsToFullAction = new QAction(this);
   64     addAction(journalsToFullAction);
   65     journalsToFullAction->setText(tr("Set Journals to Full Name"));
   66     journalsToFullAction->setStatusTip(tr("Set journals to full name"));
   67     _settingsP->setKeysequence("Shortcuts/Editor/", journalsToFullAction);
   68     journalsToAlternateFullAction = new QAction(this);
   69     addAction(journalsToAlternateFullAction);
   70     journalsToAlternateFullAction->setText(tr("Set Journals to Alternate Full Name"));
   71     journalsToAlternateFullAction->setStatusTip(tr("Set journals to alternate full name"));
   72     journalsToAbbreviatedAction = new QAction(this);
   73     addAction(journalsToAbbreviatedAction);
   74     journalsToAbbreviatedAction->setText(tr("Set Journals to Abbreviated Name"));
   75     journalsToAbbreviatedAction->setStatusTip(tr("Set journals to abbreviated name"));
   76     _settingsP->setKeysequence("Shortcuts/Editor/", journalsToAbbreviatedAction);
   77     journalsToAlternateAbbreviatedAction = new QAction(this);
   78     addAction(journalsToAlternateAbbreviatedAction);
   79     journalsToAlternateAbbreviatedAction->setText(tr("Set Journals to Alternate Abbreviated Name"));
   80     journalsToAlternateAbbreviatedAction->setStatusTip(tr("Set journals to alternate abbreviated name"));
   81 
   82     c2bUtils::addSeparator(this);
   83     updateMetadataAction = new QAction(this);
   84     addAction(updateMetadataAction);
   85     updateMetadataAction->setText(tr("Update Documents Metadata"));
   86     updateMetadataAction->setStatusTip(tr("Update document files metadata"));
   87     _settingsP->setKeysequence("Shortcuts/Editor/", updateMetadataAction);
   88 }
   89 
   90 void c2bTextEdit::setLaTeXActions()
   91 {
   92     c2bUtils::addSeparator(this);
   93     convertSelection2LaTeXAction = new QAction(this);
   94     addAction(convertSelection2LaTeXAction);
   95     convertSelection2LaTeXAction->setText(tr("Selection to LaTeX"));
   96     convertSelection2LaTeXAction->setStatusTip(tr("Convert selection to LaTeX"));
   97     _settingsP->setKeysequence("Shortcuts/Editor/", convertSelection2LaTeXAction);
   98     connect(convertSelection2LaTeXAction, SIGNAL(triggered()), this, SLOT(convert2LaTeX()));
   99     convertSelection2LaTeXAction->setEnabled(false);
  100     connect(this, SIGNAL(copyAvailable(bool)), convertSelection2LaTeXAction, SLOT(setEnabled(bool)));
  101     convertSelection2UnicodeAction = new QAction(this);
  102     addAction(convertSelection2UnicodeAction);
  103     convertSelection2UnicodeAction->setText(tr("Selection to Unicode"));
  104     convertSelection2UnicodeAction->setStatusTip(tr("Convert selection to Unicode"));
  105     _settingsP->setKeysequence("Shortcuts/Editor/", convertSelection2UnicodeAction);
  106     connect(convertSelection2UnicodeAction, SIGNAL(triggered()), this, SLOT(convert2Unicode()));
  107     convertSelection2UnicodeAction->setEnabled(false);
  108     connect(this, SIGNAL(copyAvailable(bool)), convertSelection2UnicodeAction, SLOT(setEnabled(bool)));
  109 }
  110 
  111 void c2bTextEdit::contextMenuEvent(QContextMenuEvent* qevent)
  112 {
  113     QMenu* menu = createStandardContextMenu();
  114     QList<QAction*> acts = actions();
  115     for (int i = 0; i < acts.count(); ++i)
  116         menu->addAction(acts.at(i));
  117     menu->exec(qevent->globalPos());
  118     delete menu;
  119 }
  120 
  121 void c2bTextEdit::keyPressEvent(QKeyEvent* qevent)
  122 {
  123     QPlainTextEdit::keyPressEvent(qevent);
  124     if (qevent->key() == Qt::Key_Insert)
  125     {
  126         setOverwriteMode(!overwriteMode());
  127         emit overwriteModeChanged();
  128     }
  129 }
  130 
  131 void c2bTextEdit::toggleWordWrapping()
  132 {
  133     if (lineWrapMode() == QPlainTextEdit::WidgetWidth)
  134         setLineWrapMode(QPlainTextEdit::NoWrap);
  135     else
  136         setLineWrapMode(QPlainTextEdit::WidgetWidth);
  137 }
  138 
  139 void c2bTextEdit::updatePlainText(const QString& text)
  140 {
  141     const int scrollPosition(verticalScrollBar()->value());
  142     const int cursorBlock(textCursor().blockNumber());
  143     const int cursorColumn(textCursor().columnNumber());
  144     setPlainText(text);
  145 
  146     const int lposition(document()->findBlockByNumber(cursorBlock).position());
  147     QTextCursor tcursor(textCursor());
  148     tcursor.setPosition(lposition + cursorColumn, QTextCursor::MoveAnchor);
  149     setTextCursor(tcursor);
  150     verticalScrollBar()->setValue(scrollPosition);
  151 }
  152 
  153 void c2bTextEdit::cursorPosition(int* line_number, int* line_position) const
  154 {
  155     *line_number = textCursor().blockNumber() + 1;
  156     *line_position = textCursor().positionInBlock() + 1;
  157 }
  158 
  159 void c2bTextEdit::convert2LaTeX()
  160 {
  161     QString txt(textCursor().selectedText());
  162     // Better do the long way: Convert anything to c2b, and then
  163     // translate to TeX
  164     c2bUtils::fullBibToC2b(txt);
  165     insertPlainText(c2bUtils::c2bToBib(txt));
  166 }
  167 
  168 void c2bTextEdit::convert2Unicode()
  169 {
  170     QString txt(textCursor().selectedText());
  171     // Strictly, this should be bibToC2b; but convert also
  172     // possible PNAS like, and, for consistency, HTML sub
  173     // and superscripts to TeX
  174     insertPlainText(c2bUtils::fullBibToC2b(txt));
  175 }
  176 
  177 void c2bTextEdit::loadSettings()
  178 {
  179     if (font() != _settingsP->c2bMonoFont)
  180     {
  181         setFont(_settingsP->c2bMonoFont);
  182         updateLineNumberAreaWidth();
  183     }
  184     _line_number_area->update(); // Update, otherwise number colors might not follow apply button
  185 }
  186 
  187 
  188 /***************************************************************************
  189  *  Line number area (From CodeEditor Qt's example)
  190  ***************************************************************************/
  191 
  192 int c2bTextEdit::lineNumberAreaWidth()
  193 {
  194     int digits(1);
  195     int dmax(std::max(1, blockCount()));
  196     while (dmax >= 10)
  197     {
  198         dmax /= 10;
  199         ++digits;
  200     }
  201 #if QT_VERSION >= QT_VERSION_CHECK(5, 11, 0)
  202     const int space(5 + fontMetrics().horizontalAdvance(QLatin1Char('9')) * digits);
  203 #else
  204     const int space(5 + fontMetrics().width(QLatin1Char('9')) * digits);
  205 #endif
  206     return space;
  207 }
  208 
  209 void c2bTextEdit::updateLineNumberAreaWidth()
  210 {
  211     setViewportMargins(lineNumberAreaWidth(), 0, 0, 0);
  212 }
  213 
  214 void c2bTextEdit::updateLineNumberArea(const QRect& qrect, int dy)
  215 {
  216     if (dy)
  217         _line_number_area->scroll(0, dy);
  218     else
  219         _line_number_area->update(0, qrect.y(), _line_number_area->width(), qrect.height());
  220     if (qrect.contains(viewport()->rect()))
  221         updateLineNumberAreaWidth();
  222 }
  223 
  224 void c2bTextEdit::resizeEvent(QResizeEvent* qevent)
  225 {
  226     QPlainTextEdit::resizeEvent(qevent);
  227     QRect cr = contentsRect();
  228     _line_number_area->setGeometry(QRect(cr.left(), cr.top(), lineNumberAreaWidth(), cr.height()));
  229 }
  230 
  231 void c2bTextEdit::lineNumberAreaPaintEvent(QPaintEvent* qevent)
  232 {
  233     QPainter painter(_line_number_area);
  234     painter.fillRect(qevent->rect(), _line_number_area_color);
  235     painter.setPen(*_line_number_color_p);
  236     QFont f = font();
  237     f.setPointSize(std::max(0, f.pointSize() - 3));
  238     painter.setFont(f);
  239 
  240     QTextBlock block = firstVisibleBlock();
  241     int blockNumber(block.blockNumber());
  242     int top(int(blockBoundingGeometry(block).translated(contentOffset()).top()));
  243     int bottom(top + int(blockBoundingRect(block).height()));
  244 
  245     while (block.isValid() && top <= qevent->rect().bottom())
  246     {
  247         if (block.isVisible() && bottom >= qevent->rect().top())
  248         {
  249             const QString number(QString::number(blockNumber + 1));
  250             painter.drawText(5, top + 2, _line_number_area->width(), fontMetrics().height(), Qt::AlignLeft, number);
  251         }
  252         block = block.next();
  253         top = bottom;
  254         bottom = top + (int)blockBoundingRect(block).height();
  255         ++blockNumber;
  256     }
  257 }