diff options
Diffstat (limited to 'desktop-utilities/lumina-textedit')
-rw-r--r-- | desktop-utilities/lumina-textedit/MainUI.cpp | 96 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/MainUI.h | 44 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/MainUI.ui | 150 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.cpp | 44 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/PlainTextEditor.h | 19 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/lumina-textedit.pro | 9 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/main.cpp | 17 | ||||
-rw-r--r-- | desktop-utilities/lumina-textedit/syntaxSupport.cpp | 41 |
8 files changed, 406 insertions, 14 deletions
diff --git a/desktop-utilities/lumina-textedit/MainUI.cpp b/desktop-utilities/lumina-textedit/MainUI.cpp new file mode 100644 index 00000000..71469030 --- /dev/null +++ b/desktop-utilities/lumina-textedit/MainUI.cpp @@ -0,0 +1,96 @@ +//=========================================== +// Lumina-DE source code +// Copyright (c) 2015, Ken Moore +// Available under the 3-clause BSD license +// See the LICENSE file for full details +//=========================================== +#include "MainUI.h" +#include "ui_MainUI.h" + +#include "syntaxSupport.h" + +#include <LuminaXDG.h> + +MainUI::MainUI() : QMainWindow(), ui(new Ui::MainUI){ + ui->setupUi(this); + this->setWindowTitle(tr("Text Editor")); + ui->tabWidget->clear(); + //Update the menu of available syntax highlighting modes + QStringList smodes = Custom_Syntax::availableRules(); + for(int i=0; i<smodes.length(); i++){ + ui->menuSyntax_Highlighting->addAction(smodes[i]); + } + //Setup any connections + connect(ui->actionClose, SIGNAL(triggered()), this, SLOT(close()) ); + connect(ui->actionNew_File, SIGNAL(triggered()), this, SLOT(NewFile()) ); + connect(ui->actionOpen_File, SIGNAL(triggered()), this, SLOT(OpenFile()) ); + connect(ui->actionSave_File, SIGNAL(triggered()), this, SLOT(SaveFile()) ); + connect(ui->actionSave_File_As, SIGNAL(triggered()), this, SLOT(SaveFileAs()) ); + connect(ui->menuSyntax_Highlighting, SIGNAL(triggered(QAction*)), this, SLOT(UpdateHighlighting(QAction*)) ); + updateIcons(); +} + +MainUI::~MainUI(){ + +} + +void MainUI::LoadArguments(QStringList args){ //CLI arguments + for(int i=0; i<args.length(); i++){ + + } + + if(ui->tabWidget->count()<1){ + NewFile(); + } +} + +// ================= +// PUBLIC SLOTS +//================= +void MainUI::updateIcons(){ + ui->actionClose->setIcon(LXDG::findIcon("action-close") ); + +} + +// ================= +// PRIVATE +//================= +PlainTextEditor* MainUI::currentEditor(){ + if(ui->tabWidget->count()<1){ return 0; } + return static_cast<PlainTextEditor*>( ui->tabWidget->currentWidget() ); +} + +// ================= +// PRIVATE SLOTS +//================= +//Main Actions +void MainUI::NewFile(){ + OpenFile("New-"+QString::number(ui->tabWidget->count()+1)); +} + +void MainUI::OpenFile(QString file){ + if(file.isEmpty()){ + //Prompt for a file to open + + } + if(file.isEmpty()){ return; } + PlainTextEditor *edit = new PlainTextEditor(this); + ui->tabWidget->addTab(edit, file.section("/",-1)); + edit->showLineNumbers(ui->actionLine_Numbers->isChecked()); + edit->LoadFile(file); + ui->tabWidget->setCurrentWidget(edit); +} + +void MainUI::SaveFile(){ + +} + +void MainUI::SaveFileAs(){ + +} + +void MainUI::UpdateHighlighting(QAction *act){ + PlainTextEditor *cur = currentEditor(); + if(cur==0){ return; } + cur->LoadSyntaxRule(act->text()); +} diff --git a/desktop-utilities/lumina-textedit/MainUI.h b/desktop-utilities/lumina-textedit/MainUI.h new file mode 100644 index 00000000..6ded613b --- /dev/null +++ b/desktop-utilities/lumina-textedit/MainUI.h @@ -0,0 +1,44 @@ +//=========================================== +// 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_MAIN_UI_H +#define _LUMINA_PLAIN_TEXT_EDITOR_MAIN_UI_H + +#include <QMainWindow> +#include <QStringList> + +#include "PlainTextEditor.h" + +namespace Ui{ + class MainUI; +}; + +class MainUI : public QMainWindow{ + Q_OBJECT +public: + MainUI(); + ~MainUI(); + + void LoadArguments(QStringList args); //CLI arguments + +public slots: + void updateIcons(); + +private: + Ui::MainUI *ui; + //Simplification functions + PlainTextEditor* currentEditor(); + +private slots: + //Main Actions + void NewFile(); + void OpenFile(QString file = ""); + void SaveFile(); + void SaveFileAs(); + void UpdateHighlighting(QAction*); + +}; +#endif
\ No newline at end of file diff --git a/desktop-utilities/lumina-textedit/MainUI.ui b/desktop-utilities/lumina-textedit/MainUI.ui new file mode 100644 index 00000000..01b20dc1 --- /dev/null +++ b/desktop-utilities/lumina-textedit/MainUI.ui @@ -0,0 +1,150 @@ +<?xml version="1.0" encoding="UTF-8"?> +<ui version="4.0"> + <class>MainUI</class> + <widget class="QMainWindow" name="MainUI"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>505</width> + <height>505</height> + </rect> + </property> + <property name="windowTitle"> + <string>MainWindow</string> + </property> + <widget class="QWidget" name="centralwidget"> + <layout class="QVBoxLayout" name="verticalLayout"> + <property name="leftMargin"> + <number>0</number> + </property> + <property name="topMargin"> + <number>0</number> + </property> + <property name="rightMargin"> + <number>0</number> + </property> + <property name="bottomMargin"> + <number>0</number> + </property> + <item> + <widget class="QTabWidget" name="tabWidget"> + <property name="tabsClosable"> + <bool>true</bool> + </property> + <property name="tabBarAutoHide"> + <bool>false</bool> + </property> + <widget class="QWidget" name="tab"> + <attribute name="title"> + <string>Tab 1</string> + </attribute> + </widget> + </widget> + </item> + </layout> + </widget> + <widget class="QMenuBar" name="menubar"> + <property name="geometry"> + <rect> + <x>0</x> + <y>0</y> + <width>505</width> + <height>23</height> + </rect> + </property> + <widget class="QMenu" name="menuFile"> + <property name="title"> + <string>File</string> + </property> + <addaction name="actionNew_File"/> + <addaction name="actionOpen_File"/> + <addaction name="separator"/> + <addaction name="actionSave_File"/> + <addaction name="actionSave_File_As"/> + <addaction name="separator"/> + <addaction name="actionClose"/> + </widget> + <widget class="QMenu" name="menuView"> + <property name="title"> + <string>View</string> + </property> + <widget class="QMenu" name="menuSyntax_Highlighting"> + <property name="title"> + <string>Syntax Highlighting</string> + </property> + <addaction name="action_syntax_none"/> + <addaction name="separator"/> + </widget> + <addaction name="menuSyntax_Highlighting"/> + <addaction name="actionLine_Numbers"/> + </widget> + <addaction name="menuFile"/> + <addaction name="menuView"/> + </widget> + <widget class="QStatusBar" name="statusbar"/> + <widget class="QToolBar" name="toolBar"> + <property name="windowTitle"> + <string>toolBar</string> + </property> + <property name="movable"> + <bool>false</bool> + </property> + <property name="floatable"> + <bool>false</bool> + </property> + <attribute name="toolBarArea"> + <enum>TopToolBarArea</enum> + </attribute> + <attribute name="toolBarBreak"> + <bool>false</bool> + </attribute> + <addaction name="actionNew_File"/> + <addaction name="actionOpen_File"/> + <addaction name="actionSave_File"/> + </widget> + <action name="actionLine_Numbers"> + <property name="checkable"> + <bool>true</bool> + </property> + <property name="checked"> + <bool>true</bool> + </property> + <property name="text"> + <string>Line Numbers</string> + </property> + </action> + <action name="action_syntax_none"> + <property name="text"> + <string>None</string> + </property> + </action> + <action name="actionNew_File"> + <property name="text"> + <string>New File</string> + </property> + </action> + <action name="actionOpen_File"> + <property name="text"> + <string>Open File</string> + </property> + </action> + <action name="actionSave_File"> + <property name="text"> + <string>Save File</string> + </property> + </action> + <action name="actionSave_File_As"> + <property name="text"> + <string>Save File As</string> + </property> + </action> + <action name="actionClose"> + <property name="text"> + <string>Close</string> + </property> + </action> + </widget> + <resources/> + <connections/> +</ui> diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp index adfa4d8a..92e42cf7 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.cpp +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.cpp @@ -10,21 +10,26 @@ #include <QPainter> #include <QTextBlock> +#include <LuminaUtils.h> + //============== // PUBLIC //============== PlainTextEditor::PlainTextEditor(QWidget *parent) : QPlainTextEdit(parent){ LNW = new LNWidget(this); showLNW = true; + hasChanges = false; matchleft = matchright = -1; + SYNTAX = new Custom_Syntax(this->document()); 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)) ); connect(this, SIGNAL(cursorPositionChanged()), this, SLOT(checkMatchChar()) ); - + connect(this, SIGNAL(textChanged()), this, SLOT(textChanged()) ); LNW_updateWidth(); LNW_highlightLine(); } + PlainTextEditor::~PlainTextEditor(){ } @@ -35,6 +40,33 @@ void PlainTextEditor::showLineNumbers(bool show){ LNW_updateWidth(); } +void PlainTextEditor::LoadSyntaxRule(QString type){ + SYNTAX->loadRules(type); + SYNTAX->rehighlight(); +} + +//File loading/setting options +void PlainTextEditor::LoadFile(QString filepath){ + this->setWhatsThis(filepath); + this->clear(); + SYNTAX->loadRules( Custom_Syntax::ruleForFile(filepath.section("/",-1)) ); + this->setPlainText( LUtils::readFile(filepath).join("\n") ); + hasChanges = false; +} + +void PlainTextEditor::SaveFile(){ + if( !this->whatsThis().startsWith("/") ){ + //prompt for a filename/path + + } + bool ok = LUtils::writeFile(this->whatsThis(), this->toPlainText().split("\n"), true); + if(ok){ emit FileLoaded(this->whatsThis()); } +} + +QString PlainTextEditor::currentFile(){ + return this->whatsThis(); +} + //Functions for managing the line number widget int PlainTextEditor::LNWWidth(){ //Get the number of chars we need for line numbers @@ -163,6 +195,16 @@ void PlainTextEditor::checkMatchChar(){ else if(ch==QChar(')')){ highlightMatch(QChar('('),false, pos); } else if(ch==QChar('{')){ highlightMatch(QChar('}'),true, pos); } else if(ch==QChar('}')){ highlightMatch(QChar('{'),false, pos); } + else if(ch==QChar('[')){ highlightMatch(QChar(']'),true, pos); } + else if(ch==QChar(']')){ highlightMatch(QChar('['),false, pos); } +} + +//Functions for notifying the parent widget of changes +void PlainTextEditor::textChanged(){ + if(!hasChanges){ + hasChanges = true; + emit UnsavedChanges( this->whatsThis() ); + } } //================== diff --git a/desktop-utilities/lumina-textedit/PlainTextEditor.h b/desktop-utilities/lumina-textedit/PlainTextEditor.h index 81574b32..291f09aa 100644 --- a/desktop-utilities/lumina-textedit/PlainTextEditor.h +++ b/desktop-utilities/lumina-textedit/PlainTextEditor.h @@ -12,6 +12,8 @@ #include <QResizeEvent> #include <QPaintEvent> +#include "syntaxSupport.h" + //QPlainTextEdit subclass for providing the actual text editor functionality class PlainTextEditor : public QPlainTextEdit{ Q_OBJECT @@ -21,6 +23,12 @@ public: //Functions for setting up the editor void showLineNumbers(bool show = true); + void LoadSyntaxRule(QString type); + + //File loading/setting options + void LoadFile(QString filepath); + void SaveFile(); + QString currentFile(); //Functions for managing the line number widget (internal - do not need to run directly) int LNWWidth(); //replacing the LNW size hint detection @@ -29,12 +37,16 @@ public: private: QWidget *LNW; //Line Number Widget bool showLNW; + //Syntax Highlighting class + Custom_Syntax *SYNTAX; //Bracket/Perentheses matching functions int matchleft, matchright; //positions within the document void clearMatchData(); void highlightMatch(QChar ch, bool forward, int fromPos); + //Flags to keep track of changes + bool hasChanges; private slots: //Functions for managing the line number widget void LNW_updateWidth(); // Tied to the QPlainTextEdit::blockCountChanged() signal @@ -42,9 +54,16 @@ private slots: void LNW_update(const QRect&, int); // Tied to the QPlainTextEdit::updateRequest() signal //Function for running the matching routine void checkMatchChar(); + //Functions for notifying the parent widget of changes + void textChanged(); protected: void resizeEvent(QResizeEvent *ev); + +signals: + void UnsavedChanges(QString); //filename + void FileLoaded(QString); + }; //=========================================================== diff --git a/desktop-utilities/lumina-textedit/lumina-textedit.pro b/desktop-utilities/lumina-textedit/lumina-textedit.pro index 148dfa37..7896d20c 100644 --- a/desktop-utilities/lumina-textedit/lumina-textedit.pro +++ b/desktop-utilities/lumina-textedit/lumina-textedit.pro @@ -5,13 +5,16 @@ QT += core gui widgets TARGET = lumina-textedit target.path = $${L_BINDIR} -HEADERS += PlainTextEditor.h \ +HEADERS += MainUI.h \ + PlainTextEditor.h \ syntaxSupport.h SOURCES += main.cpp \ - PlainTextEditor.cpp \ - syntaxSupport.cpp + MainUI.cpp \ + PlainTextEditor.cpp \ + syntaxSupport.cpp +FORMS += MainUI.ui LIBS += -lLuminaUtils diff --git a/desktop-utilities/lumina-textedit/main.cpp b/desktop-utilities/lumina-textedit/main.cpp index 899382c7..17fbc6e1 100644 --- a/desktop-utilities/lumina-textedit/main.cpp +++ b/desktop-utilities/lumina-textedit/main.cpp @@ -10,8 +10,7 @@ #include <LuminaThemes.h> #include <LuminaUtils.h> -#include "PlainTextEditor.h" -#include "syntaxSupport.h" +#include "MainUI.h" int main(int argc, char *argv[]) { LTHEME::LoadCustomEnvSettings(); @@ -19,14 +18,14 @@ int main(int argc, char *argv[]) { LUtils::LoadTranslation(&a, "lumina-textedit"); //Now go ahead and setup the app LuminaThemeEngine theme(&a); - + QStringList args; + for(int i=1; i<argc; i++){ + args << QString(argv[i]); + } //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()) ); - + MainUI W; + QObject::connect(&theme, SIGNAL(updateIcons()), &W, SLOT(updateIcons()) ); + W.LoadArguments(args); W.show(); return a.exec(); } diff --git a/desktop-utilities/lumina-textedit/syntaxSupport.cpp b/desktop-utilities/lumina-textedit/syntaxSupport.cpp index 38ca4d3f..093ca52a 100644 --- a/desktop-utilities/lumina-textedit/syntaxSupport.cpp +++ b/desktop-utilities/lumina-textedit/syntaxSupport.cpp @@ -9,7 +9,7 @@ QStringList Custom_Syntax::availableRules(){ QStringList avail; avail << "C++"; - + avail << "Python"; return avail; } @@ -61,5 +61,44 @@ void Custom_Syntax::loadRules(QString type){ srule.startPattern = QRegExp("/\\*"); srule.endPattern = QRegExp("\\*/"); splitrules << srule; + + }else if(type=="Python"){ + //Keywords + QStringList keywords; + keywords << "and" << "as" << "assert" << "break" << "class" << "continue" << "def" << "del" \ + << "elif" << "else" << "except" << "exec" << "finally" << "for" << "from" \ + << "global" << "if" << "import" << "in" << "is" << "lambda" << "not" \ + << "or" << "pass" << "print" << "raise" << "return" << "try" << "while" << "with" << "yield"; + + SyntaxRule rule; + rule.format.setForeground(Qt::darkBlue); + rule.format.setFontWeight(QFont::Bold); + for(int i=0; i<keywords.length(); i++){ + rule.pattern = QRegExp("\\b"+keywords[i]+"\\b"); //turn each keyword into a QRegExp and insert the rule + rules << rule; + } + //Class Names + //rule.format.setForeground(Qt::darkMagenta); + //rule.pattern = QRegExp("\\bQ[A-Za-z]+\\b"); + //rules << rule; + //Quotes + rule.format.setForeground(Qt::red); + rule.format.setFontWeight(QFont::Normal); + rule.pattern = QRegExp("\".*\""); + rules << rule; + //Functions + rule.format.setForeground(Qt::blue); + rule.pattern = QRegExp("\\b[A-Za-z0-9_]+(?=\\()"); + rules << rule; + //Comment (single line) + rule.format.setForeground(Qt::green); + rule.pattern = QRegExp("#[^\n]*"); + rules << rule; + //Comment (multi-line) + //SyntaxRuleSplit srule; + //srule.format = rule.format; //re-use the single-line comment format + //srule.startPattern = QRegExp("/\\*"); + //srule.endPattern = QRegExp("\\*/"); + //splitrules << srule; } }
\ No newline at end of file |