Debian Codegraph @

ruqplaintextedit.cpp

raw symbols SCIP
1 /****************************************************************************
2 ** SASM - simple IDE for assembler development
3 ** Copyright (C) 2013 Dmitriy Manushin
4 ** Contact: site: http://dman95.github.io/SASM/
5 ** e-mail: [email protected]
6 **
7 ** This file is part of SASM.
8 **
9 ** SASM is free software: you can redistribute it and/or modify
10 ** it under the terms of the GNU General Public License as published by
11 ** the Free Software Foundation, either version 3 of the License, or
12 ** (at your option) any later version.
13 **
14 ** SASM is distributed in the hope that it will be useful,
15 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ** GNU General Public License for more details.
18 **
19 ** You should have received a copy of the GNU General Public License
20 ** along with SASM. If not, see <http://www.gnu.org/licenses/>.
21 **
22 ** Этот файл — часть SASM.
23 **
24 ** SASM - свободная программа: вы можете перераспространять ее и/или
25 ** изменять ее на условиях Стандартной общественной лицензии GNU в том виде,
26 ** в каком она была опубликована Фондом свободного программного обеспечения;
27 ** либо версии 3 лицензии, либо (по вашему выбору) любой более поздней
28 ** версии.
29 **
30 ** SASM распространяется в надежде, что она будет полезной,
31 ** но БЕЗО ВСЯКИХ ГАРАНТИЙ; даже без неявной гарантии ТОВАРНОГО ВИДА
32 ** или ПРИГОДНОСТИ ДЛЯ ОПРЕДЕЛЕННЫХ ЦЕЛЕЙ. Подробнее см. в Стандартной
33 ** общественной лицензии GNU.
34 **
35 ** Вы должны были получить копию Стандартной общественной лицензии GNU
36 ** вместе с этой программой. Если это не так, см.
37 ** <http://www.gnu.org/licenses/>.)
38 **
39 ****************************************************************************/
40
41 #include "ruqplaintextedit.h"
42
43 /**
44 * @file ruplaintextedit.cpp
45 * Implements the text editor base class
46 */
47
48 RuQPlainTextEdit::RuQPlainTextEdit(QWidget *parent) :
49 QPlainTextEdit(parent)
50 {
51 QSettings settings("SASM Project", "SASM");
52 QPalette palette = this->palette();
53 palette.setColor(QPalette::Base, settings.value("backgroundcolor", QColor(255, 255, 255)).value<QColor>());
54 palette.setColor(QPalette::Text, settings.value("fontcolor", QColor(30, 30, 30)).value<QColor>());
55 setPalette(palette);
56
57 setDebugDisabled();
58
59 QSettings keySettings(Common::applicationDataPath() + "/keys.ini", QSettings::IniFormat);
60
61 commentAction = new QAction(tr("Comment"), this);
62 QString key = keySettings.value("comment", "default").toString();
63 QKeySequence stdKey = QKeySequence(QString("Shift+Ctrl+A"));
64 if (key == "default")
65 key = stdKey.toString();
66 commentAction->setShortcut(key);
67 connect(commentAction, SIGNAL(triggered()), this, SLOT(commentSelectedCode()));
68
69 uncommentAction = new QAction(tr("Remove comment"), this);
70 key = keySettings.value("uncomment", "default").toString();
71 stdKey = QKeySequence(QString("Shift+Ctrl+Q"));
72 if (key == "default")
73 key = stdKey.toString();
74 uncommentAction->setShortcut(key);
75 connect(uncommentAction, SIGNAL(triggered()), this, SLOT(uncommentSelectedCode()));
76
77 undoAction = new QAction(tr("Undo"), this);
78 undoAction->setShortcut(QKeySequence::Undo);
79 connect(undoAction, SIGNAL(triggered()), this, SLOT(undo()));
80
81 redoAction = new QAction(tr("Redo"), this);
82 redoAction->setShortcut(QKeySequence::Redo);
83 connect(redoAction, SIGNAL(triggered()), this, SLOT(redo()));
84
85 cutAction = new QAction(tr("Cut"), this);
86 cutAction->setShortcut(QKeySequence::Cut);
87 connect(cutAction, SIGNAL(triggered()), this, SLOT(cut()));
88
89 copyAction = new QAction(tr("Copy"), this);
90 copyAction->setShortcut(QKeySequence::Copy);
91 connect(copyAction, SIGNAL(triggered()), this, SLOT(copy()));
92
93 pasteAction = new QAction(tr("Paste"), this);
94 pasteAction->setShortcut(QKeySequence::Paste);
95 connect(pasteAction, SIGNAL(triggered()), this, SLOT(paste()));
96
97 deleteAction = new QAction(tr("Delete"), this);
98 deleteAction->setShortcut(QKeySequence::Delete);
99 connect(deleteAction, SIGNAL(triggered()), this, SLOT(deleteSelected()));
100
101 selectAllAction = new QAction(tr("Select all"), this);
102 selectAllAction->setShortcut(QKeySequence::SelectAll);
103 connect(selectAllAction, SIGNAL(triggered()), this, SLOT(selectAll()));
104
105 addWatchAction = new QAction(this);
106 connect(addWatchAction, SIGNAL(triggered()), this, SLOT(addWatch()));
107
108 undoAction->setEnabled(false);
109 redoAction->setEnabled(false);
110 connect(this, SIGNAL(undoAvailable(bool)), undoAction, SLOT(setEnabled(bool)));
111 connect(this, SIGNAL(redoAvailable(bool)), redoAction, SLOT(setEnabled(bool)));
112 }
113
114 QMenu * RuQPlainTextEdit::createMenu()
115 {
116 QMenu *menu = new QMenu;
117 QTextCursor textCursor = this->textCursor();
118
119 //! if nothing selected
120 if (textCursor.selectionEnd() - textCursor.selectionStart() <= 0) {
121 commentAction->setEnabled(false);
122 uncommentAction->setEnabled(false);
123 cutAction->setEnabled(false);
124 copyAction->setEnabled(false);
125 deleteAction->setEnabled(false);
126 } else {
127 commentAction->setEnabled(true);
128 uncommentAction->setEnabled(true);
129 cutAction->setEnabled(true);
130 copyAction->setEnabled(true);
131 deleteAction->setEnabled(true);
132 }
133
134 if (isReadOnly()) {
135 cutAction->setVisible(false);
136 pasteAction->setVisible(false);
137 deleteAction->setVisible(false);
138 } else {
139 cutAction->setVisible(true);
140 pasteAction->setVisible(true);
141 deleteAction->setVisible(true);
142 }
143
144 QString variableName = variableOnCurrentLine().name;
145 if (variableName.isEmpty()) {
146 addWatchAction->setVisible(false);
147 } else {
148 addWatchAction->setVisible(true);
149 addWatchAction->setText(tr("Watch '%1'").arg(variableName));
150 if (!debugEnabled) {
151 addWatchAction->setEnabled(false);
152 } else {
153 addWatchAction->setEnabled(true);
154 }
155 }
156
157 menu->addAction(addWatchAction);
158 menu->addSeparator();
159 menu->addAction(commentAction);
160 menu->addAction(uncommentAction);
161 menu->addSeparator();
162 menu->addAction(undoAction);
163 menu->addAction(redoAction);
164 menu->addSeparator();
165 menu->addAction(cutAction);
166 menu->addAction(copyAction);
167 menu->addAction(pasteAction);
168 menu->addAction(deleteAction);
169 menu->addSeparator();
170 menu->addAction(selectAllAction);
171
172 return menu;
173 }
174
175 void RuQPlainTextEdit::contextMenuEvent(QContextMenuEvent *e)
176 {
177 QMouseEvent leftClick(QEvent::MouseButtonPress, e->pos(), Qt::LeftButton, Qt::NoButton, Qt::NoModifier);
178 mousePressEvent(&leftClick);
179 contextMenu = createMenu();
180 contextMenu->exec(e->globalPos());
181 if (!contextMenu.isNull())
182 delete contextMenu;
183 }
184
185 void RuQPlainTextEdit::deleteSelected()
186 {
187 QTextCursor textCursor = this->textCursor();
188 textCursor.removeSelectedText();
189 }
190
191 void RuQPlainTextEdit::commentSelectedCode()
192 {
193 QTextCursor cursor = textCursor();
194 QString selected = cursor.selectedText().replace(QString(QChar::ParagraphSeparator),
195 QString(QChar::ParagraphSeparator) + QString(';'));
196 selected.insert(0, QChar(';'));
197 cursor.removeSelectedText();
198 cursor.insertText(selected);
199 }
200
201 void RuQPlainTextEdit::uncommentSelectedCode()
202 {
203 QTextCursor cursor = textCursor();
204 QString selected = cursor.selectedText().replace(QString(QChar::ParagraphSeparator) + QString(';'),
205 QString(QChar::ParagraphSeparator));
206 if (selected[0] == QChar(';'))
207 selected.remove(0, 1);
208 cursor.removeSelectedText();
209 cursor.insertText(selected);
210 }
211
212 RuQPlainTextEdit::Watch RuQPlainTextEdit::variableOnCurrentLine()
213 {
214 QTextBlock block = document()->findBlock(textCursor().position());
215 QString text = block.text();
216 QStringList memoryPatterns;
217 memoryPatterns << "\\bresb\\b" << "\\bresw\\b" << "\\bresd\\b" <<
218 "\\bresq\\b" << "\\brest\\b" << "\\breso\\b" <<
219 "\\bresy\\b" << "\\bddq\\b" << "\\bresdq\\b" <<
220 "\\bdb\\b" << "\\bdw\\b" << "\\bdd\\b" <<
221 "\\bdq\\b" << "\\bdt\\b" << "\\bdo\\b" <<
222 "\\bdy\\b" << "\\bequ\\b";
223 bool isVariableLine = false;
224 foreach (const QString &pattern, memoryPatterns)
225 if (text.indexOf(QRegExp(pattern)) != -1) {
226 isVariableLine = true;
227 break;
228 }
229 QString variableName;
230 int variableSize = 0;
231 int arraySize = 0;
232 if (isVariableLine) {
233 QTextStream line(&text);
234 line.skipWhiteSpace();
235 line >> variableName;
236 line.skipWhiteSpace();
237 QString temp = 0;
238 line >> temp;
239 bool isArray = false;
240 if (temp.indexOf("res") != -1)
241 isArray = true;
242 temp = temp.right(1);
243 if (temp == "q")
244 variableSize = 3;
245 if (temp == "b")
246 variableSize = 2;
247 if (temp == "w")
248 variableSize = 1;
249 line.skipWhiteSpace();
250 if (isArray) {
251 line >> arraySize;
252 }
253 }
254 Watch w;
255 w.name = variableName;
256 w.type = 0;
257 w.size = variableSize;
258 if (arraySize == 1)
259 arraySize = 0;
260 w.arraySize = arraySize;
261 w.address = false;
262 return w;
263 }
264
265 void RuQPlainTextEdit::addWatch()
266 {
267 emit addWatchSignal(variableOnCurrentLine());
268 }
269
270 void RuQPlainTextEdit::setDebugDisabled()
271 {
272 debugEnabled = false;
273 }
274
275 void RuQPlainTextEdit::setDebugEnabled()
276 {
277 debugEnabled = true;
278 }
279
280 RuQPlainTextEdit::~RuQPlainTextEdit()
281 {
282 delete commentAction;
283 delete uncommentAction;
284 delete undoAction;
285 delete redoAction;
286 delete cutAction;
287 delete copyAction;
288 delete pasteAction;
289 delete deleteAction;
290 delete selectAllAction;
291 delete addWatchAction;
292 }