diff options
author | Ken Moore <moorekou@gmail.com> | 2016-04-14 12:49:55 -0400 |
---|---|---|
committer | Ken Moore <moorekou@gmail.com> | 2016-04-14 12:49:55 -0400 |
commit | 1a1e43c175bcba94d84dbb1368329fe45827bb16 (patch) | |
tree | 64adb89cdb69c5900a783e77ad5e6d057d4e2f78 | |
parent | Fix up some selection/cursor handling so the user cannot change the current c... (diff) | |
download | lumina-1a1e43c175bcba94d84dbb1368329fe45827bb16.tar.gz lumina-1a1e43c175bcba94d84dbb1368329fe45827bb16.tar.bz2 lumina-1a1e43c175bcba94d84dbb1368329fe45827bb16.zip |
Add a start for a Qt text editor (mainly for playing around - but it could turn into something useful down the road).
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.cpp | 114 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.h | 68 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/lumina-textedit.pro | 92 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/main.cpp | 32 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/syntaxSupport.cpp | 26 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/syntaxSupport.h | 45 |
6 files changed, 377 insertions, 0 deletions
diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp new file mode 100644 index 00000000..7b105d20 --- /dev/null +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp @@ -0,0 +1,114 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "PlainTextEditor.h" + +#include <QColor> +#include <QPainter> +#include <QTextBlock> + +//============== +// PUBLIC +//============== +PlainTextEditor::PlainTextEditor(QWidget *parent) : QPlainTextEdit(parent){ + LNW = new LNWidget(this); + showLNW = true; + connect(this, SIGNAL(blockCountChanged(int)), this, SLOT(LNW_updateWidth()) ); + connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(LNW_highlightLine()) ); + connect(this, SIGNAL(updateRequest(const QRect&, int)), this, SLOT(LNW_update(const QRect&, int)) ); + + LNW_updateWidth(); + LNW_highlightLine(); +} +PlainTextEditor::~PlainTextEditor(){ + +} + +void PlainTextEditor::showLineNumbers(bool show){ + showLNW = show; + LNW->setVisible(show); + LNW_updateWidth(); +} + +//Functions for managing the line number widget +int PlainTextEditor::LNWWidth(){ + //Get the number of chars we need for line numbers + int lines = this->blockCount(); + if(lines<1){ lines = 1; } + int chars = 1; + while(lines>=10){ chars++; lines/=10; } + return (this->fontMetrics().width("9")*chars + 4); //make sure to add a tiny bit of padding +} + +void PlainTextEditor::paintLNW(QPaintEvent *ev){ + QPainter P(LNW); + //First set the background color + P.fillRect(ev->rect(), QColor("lightgrey")); + //Now determine which line numbers to show (based on the current viewport) + QTextBlock block = this->firstVisibleBlock(); + int bTop = blockBoundingGeometry(block).translated(contentOffset()).top(); + int bBottom; + //Now loop over the blocks (lines) and write in the numbers + P.setPen(Qt::black); //setup the font color + while(block.isValid() && bTop<=ev->rect().bottom()){ //ensure block below top of viewport + bBottom = bTop+blockBoundingRect(block).height(); + if(block.isVisible() && bBottom >= ev->rect().top()){ //ensure block above bottom of viewport + P.drawText(0,bTop, LNW->width(), this->fontMetrics().height(), Qt::AlignRight, QString::number(block.blockNumber()+1) ); + } + //Go to the next block + block = block.next(); + bTop = bBottom; + } +} + +//============== +// PRIVATE +//============== + +//=================== +// PRIVATE SLOTS +//=================== +//Functions for managing the line number widget +void PlainTextEditor::LNW_updateWidth(){ + if(showLNW){ + this->setViewportMargins( LNWWidth(), 0, 0, 0); //the LNW is contained within the left margin + }else{ + this->setViewportMargins( 0, 0, 0, 0); //the LNW is contained within the left margin + } +} + +void PlainTextEditor::LNW_highlightLine(){ + if(this->isReadOnly()){ return; } + QColor highC = QColor(0,0,0,50); //just darken the line a bit + QTextEdit::ExtraSelection sel; + sel.format.setBackground(highC); + sel.format.setProperty(QTextFormat::FullWidthSelection, true); + sel.cursor = this->textCursor(); + sel.cursor.clearSelection(); //just in case it already has one + setExtraSelections( QList<QTextEdit::ExtraSelection>() << sel ); +} + +void PlainTextEditor::LNW_update(const QRect &rect, int dy){ + if(dy!=0){ LNW->scroll(0,dy); } //make sure to scroll the line widget the same amount as the editor + else{ + //Some other reason we need to repaint the widget + LNW->update(0,rect.y(), LNW->width(), rect.height()); //also repaint the LNW in the same area + } + if(rect.contains(this->viewport()->rect())){ + //Something in the currently-viewed area needs updating - make sure the LNW width is still correct + LNW_updateWidth(); + } +} + +//================== +// PROTECTED +//================== +void PlainTextEditor::resizeEvent(QResizeEvent *ev){ + QPlainTextEdit::resizeEvent(ev); //do the normal resize processing + //Now re-adjust the placement of the LNW (within the left margin area) + QRect cGeom = this->contentsRect(); + LNW->setGeometry( QRect(cGeom.left(), cGeom.top(), LNWWidth(), cGeom.height()) ); +} diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.h b/desktop-utilities/lumina-textedit/PlainTextEditor.h new file mode 100644 index 00000000..46841e59 --- /dev/null +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.h @@ -0,0 +1,68 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#ifndef _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H +#define _LUMINA_PLAIN_TEXT_EDITOR_WIDGET_H + +#include <QPlainTextEdit> +#include <QWidget> +#include <QResizeEvent> +#include <QPaintEvent> + +//QPlainTextEdit subclass for providing the actual text editor functionality +class PlainTextEditor : public QPlainTextEdit{ + Q_OBJECT +public: + PlainTextEditor(QWidget *parent = 0); + ~PlainTextEditor(); + + //Functions for setting up the editor + void showLineNumbers(bool show = true); + + //Functions for managing the line number widget (internal - do not need to run directly) + int LNWWidth(); //replacing the LNW size hint detection + void paintLNW(QPaintEvent *ev); //forwarded from the LNW paint event + +private: + QWidget *LNW; //Line Number Widget + bool showLNW; + +private slots: + //Functions for managing the line number widget + void LNW_updateWidth(); // Tied to the QPlainTextEdit::blockCountChanged() signal + void LNW_highlightLine(); // Tied to the QPlainTextEdit::cursorPositionChanged() signal + void LNW_update(const QRect&, int); // Tied to the QPlainTextEdit::updateRequest() signal + +protected: + void resizeEvent(QResizeEvent *ev); +}; + +//=========================================================== +// Small Widget for painting the line numbers in the PlainTextEditor +//=========================================================== +class LNWidget : public QWidget{ + Q_OBJECT +private: + PlainTextEditor *TE; +public: + LNWidget( PlainTextEditor *edit) : QWidget(edit){ + TE = edit; + } + ~LNWidget(){} + //Replace the virtual QWidget size hint function + // since the main text editor controls the size/location of this widget + QSize sizeHint() const{ + return QSize(TE->LNWWidth(),0); + } +protected: + //Replace the virtual QWidget paint event function + // since the main text editor control the size/location of this widget + void paintEvent(QPaintEvent *ev){ + TE->paintLNW(ev); + } +}; +#endif + diff --git a/desktop-utilities/lumina-textedit/lumina-textedit.pro b/desktop-utilities/lumina-textedit/lumina-textedit.pro new file mode 100644 index 00000000..148dfa37 --- /dev/null +++ b/desktop-utilities/lumina-textedit/lumina-textedit.pro @@ -0,0 +1,92 @@ +include("$${PWD}/../../OS-detect.pri") + +QT += core gui widgets + +TARGET = lumina-textedit +target.path = $${L_BINDIR} + +HEADERS += PlainTextEditor.h \ + syntaxSupport.h + +SOURCES += main.cpp \ + PlainTextEditor.cpp \ + syntaxSupport.cpp + + +LIBS += -lLuminaUtils + + +DEPENDPATH += ../../libLumina + +TRANSLATIONS = i18n/lumina-textedit_af.ts \ + i18n/lumina-textedit_ar.ts \ + i18n/lumina-textedit_az.ts \ + i18n/lumina-textedit_bg.ts \ + i18n/lumina-textedit_bn.ts \ + i18n/lumina-textedit_bs.ts \ + i18n/lumina-textedit_ca.ts \ + i18n/lumina-textedit_cs.ts \ + i18n/lumina-textedit_cy.ts \ + i18n/lumina-textedit_da.ts \ + i18n/lumina-textedit_de.ts \ + i18n/lumina-textedit_el.ts \ + i18n/lumina-textedit_en_GB.ts \ + i18n/lumina-textedit_en_ZA.ts \ + i18n/lumina-textedit_es.ts \ + i18n/lumina-textedit_et.ts \ + i18n/lumina-textedit_eu.ts \ + i18n/lumina-textedit_fa.ts \ + i18n/lumina-textedit_fi.ts \ + i18n/lumina-textedit_fr.ts \ + i18n/lumina-textedit_fr_CA.ts \ + i18n/lumina-textedit_gl.ts \ + i18n/lumina-textedit_he.ts \ + i18n/lumina-textedit_hi.ts \ + i18n/lumina-textedit_hr.ts \ + i18n/lumina-textedit_hu.ts \ + i18n/lumina-textedit_id.ts \ + i18n/lumina-textedit_is.ts \ + i18n/lumina-textedit_it.ts \ + i18n/lumina-textedit_ja.ts \ + i18n/lumina-textedit_ka.ts \ + i18n/lumina-textedit_ko.ts \ + i18n/lumina-textedit_lt.ts \ + i18n/lumina-textedit_lv.ts \ + i18n/lumina-textedit_mk.ts \ + i18n/lumina-textedit_mn.ts \ + i18n/lumina-textedit_ms.ts \ + i18n/lumina-textedit_mt.ts \ + i18n/lumina-textedit_nb.ts \ + i18n/lumina-textedit_nl.ts \ + i18n/lumina-textedit_pa.ts \ + i18n/lumina-textedit_pl.ts \ + i18n/lumina-textedit_pt.ts \ + i18n/lumina-textedit_pt_BR.ts \ + i18n/lumina-textedit_ro.ts \ + i18n/lumina-textedit_ru.ts \ + i18n/lumina-textedit_sk.ts \ + i18n/lumina-textedit_sl.ts \ + i18n/lumina-textedit_sr.ts \ + i18n/lumina-textedit_sv.ts \ + i18n/lumina-textedit_sw.ts \ + i18n/lumina-textedit_ta.ts \ + i18n/lumina-textedit_tg.ts \ + i18n/lumina-textedit_th.ts \ + i18n/lumina-textedit_tr.ts \ + i18n/lumina-textedit_uk.ts \ + i18n/lumina-textedit_uz.ts \ + i18n/lumina-textedit_vi.ts \ + i18n/lumina-textedit_zh_CN.ts \ + i18n/lumina-textedit_zh_HK.ts \ + i18n/lumina-textedit_zh_TW.ts \ + i18n/lumina-textedit_zu.ts + +dotrans.path=$${L_SHAREDIR}/Lumina-DE/i18n/ +dotrans.extra=cd i18n && $${LRELEASE} -nounfinished *.ts && cp *.qm $(INSTALL_ROOT)$${L_SHAREDIR}/Lumina-DE/i18n/ + +INSTALLS += target dotrans + +NO_I18N{ + INSTALLS -= dotrans +} + diff --git a/desktop-utilities/lumina-textedit/main.cpp b/desktop-utilities/lumina-textedit/main.cpp new file mode 100644 index 00000000..899382c7 --- /dev/null +++ b/desktop-utilities/lumina-textedit/main.cpp @@ -0,0 +1,32 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include <QApplication> +#include <QDebug> + +#include <LuminaThemes.h> +#include <LuminaUtils.h> + +#include "PlainTextEditor.h" +#include "syntaxSupport.h" + +int main(int argc, char *argv[]) { + LTHEME::LoadCustomEnvSettings(); + QApplication a(argc, argv); + LUtils::LoadTranslation(&a, "lumina-textedit"); + //Now go ahead and setup the app + LuminaThemeEngine theme(&a); + + //Now start the window + PlainTextEditor W; + Custom_Syntax *syntax = new Custom_Syntax(W.document()); + syntax->loadRules("C++"); + W.showLineNumbers(true); + //QObject::connect(&theme, SIGNAL(updateIcons()), &W, SLOT(updateIcons()) ); + + W.show(); + return a.exec(); +} diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.cpp b/desktop-utilities/lumina-textedit/syntaxSupport.cpp new file mode 100644 index 00000000..27664c52 --- /dev/null +++ b/desktop-utilities/lumina-textedit/syntaxSupport.cpp @@ -0,0 +1,26 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "syntaxSupport.h" +QStringList Custom_Syntax::availableRules(){ + QStringList avail; + avail << "C++"; + + return avail; +} + +QString Custom_Syntax::ruleForFile(QString filename){ + QString suffix = filename.section(".",-1); + if(suffix=="cpp" || suffix=="hpp" || suffix=="c" || suffix=="h"){ return "C++"; } + return ""; +} + +void Custom_Syntax::loadRules(QString type){ + rules.clear(); + if(type=="C++"){ + + } +}
\ No newline at end of file diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.h b/desktop-utilities/lumina-textedit/syntaxSupport.h new file mode 100644 index 00000000..e8944aa4 --- /dev/null +++ b/desktop-utilities/lumina-textedit/syntaxSupport.h @@ -0,0 +1,45 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#ifndef _LUMINA_SYNTAX_HIGHLIGHER_CPP_H +#define _LUMINA_SYNTAX_HIGHLIGHER_CPP_H + +#include <QSyntaxHighlighter> +#include <QTextDocument> +#include <QTextCharFormat> +#include <QString> + +struct SyntaxRule{ + QRegExp pattern; + QTextCharFormat format; +}; + +class Custom_Syntax : public QSyntaxHighlighter{ + Q_OBJECT +private: + QVector<SyntaxRule> rules; +public: + Custom_Syntax(QTextDocument *parent = 0) : QSyntaxHighlighter(parent){ + } + ~Custom_Syntax(){} + + static QStringList availableRules(); + static QString ruleForFile(QString filename); + void loadRules(QString type); +protected: + void highlightBlock(const QString &text){ + for(int i=0; i<rules.length(); i++){ + QRegExp patt(rules[i].pattern); //need a copy of the rule's pattern (will be changing it below) + int index = patt.indexIn(text); + while(index>=0){ + int len = patt.matchedLength(); + setFormat(index, len, rules[i].format); + index = patt.indexIn(text, index+len); //go to the next match + } + } + } +}; +#endif
\ No newline at end of file |