| 1 |
/*************************************************************************** |
| 2 |
* Copyright (c) 2007 Werner Mayer <wmayer[at]users.sourceforge.net> * |
| 3 |
* * |
| 4 |
* This file is part of the FreeCAD CAx development system. * |
| 5 |
* * |
| 6 |
* This library is free software; you can redistribute it and/or * |
| 7 |
* modify it under the terms of the GNU Library General Public * |
| 8 |
* License as published by the Free Software Foundation; either * |
| 9 |
* version 2 of the License, or (at your option) any later version. * |
| 10 |
* * |
| 11 |
* This library is distributed in the hope that it will be useful, * |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 |
* GNU Library General Public License for more details. * |
| 15 |
* * |
| 16 |
* You should have received a copy of the GNU Library General Public * |
| 17 |
* License along with this library; see the file COPYING.LIB. If not, * |
| 18 |
* write to the Free Software Foundation, Inc., 59 Temple Place, * |
| 19 |
* Suite 330, Boston, MA 02111-1307, USA * |
| 20 |
* * |
| 21 |
***************************************************************************/ |
| 22 |
|
| 23 |
#include <QApplication> |
| 24 |
#include <QCheckBox> |
| 25 |
#include <QClipboard> |
| 26 |
#include <QDateTime> |
| 27 |
#include <QHBoxLayout> |
| 28 |
#include <QVBoxLayout> |
| 29 |
#include <QLineEdit> |
| 30 |
#include <QMessageBox> |
| 31 |
#include <QPrinter> |
| 32 |
#include <QPrintDialog> |
| 33 |
#include <QPlainTextEdit> |
| 34 |
#include <QPrintPreviewDialog> |
| 35 |
#include <QSpacerItem> |
| 36 |
#include <QStyle> |
| 37 |
#include <QTextCursor> |
| 38 |
#include <QTextDocument> |
| 39 |
#include <QTextStream> |
| 40 |
#include <QTimer> |
| 41 |
#include <QToolButton> |
| 42 |
|
| 43 |
#include "EditorView.h" |
| 44 |
#include "Application.h" |
| 45 |
#include "FileDialog.h" |
| 46 |
#include "Macro.h" |
| 47 |
#include "MainWindow.h" |
| 48 |
#include "PythonEditor.h" |
| 49 |
#include "PythonTracing.h" |
| 50 |
#include "WaitCursor.h" |
| 51 |
|
| 52 |
#include <Base/Exception.h> |
| 53 |
#include <Base/Interpreter.h> |
| 54 |
#include <Base/Parameter.h> |
| 55 |
|
| 56 |
#include <Gui/PreferencePages/DlgSettingsPDF.h> |
| 57 |
|
| 58 |
|
| 59 |
using namespace Gui; |
| 60 |
namespace Gui |
| 61 |
{ |
| 62 |
class EditorViewP |
| 63 |
{ |
| 64 |
public: |
| 65 |
TextEdit* textEdit; |
| 66 |
SearchBar* searchBar; |
| 67 |
QString fileName; |
| 68 |
EditorView::DisplayName displayName; |
| 69 |
QTimer* activityTimer; |
| 70 |
qint64 timeStamp; |
| 71 |
bool lock; |
| 72 |
bool aboutToClose; |
| 73 |
QStringList undos; |
| 74 |
QStringList redos; |
| 75 |
}; |
| 76 |
} // namespace Gui |
| 77 |
|
| 78 |
// ------------------------------------------------------- |
| 79 |
|
| 80 |
/* TRANSLATOR Gui::EditorView */ |
| 81 |
|
| 82 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::EditorView, Gui::MDIView) |
| 83 |
|
| 84 |
/** |
| 85 |
* Constructs a EditorView which is a child of 'parent', with the |
| 86 |
* name 'name'. |
| 87 |
*/ |
| 88 |
EditorView::EditorView(TextEdit* editor, QWidget* parent) |
| 89 |
: MDIView(nullptr, parent, Qt::WindowFlags()) |
| 90 |
, WindowParameter("Editor") |
| 91 |
{ |
| 92 |
d = new EditorViewP; |
| 93 |
d->lock = false; |
| 94 |
d->aboutToClose = false; |
| 95 |
d->displayName = EditorView::FullName; |
| 96 |
|
| 97 |
// create the editor first |
| 98 |
d->textEdit = editor; |
| 99 |
d->textEdit->setLineWrapMode(QPlainTextEdit::NoWrap); |
| 100 |
|
| 101 |
d->searchBar = new SearchBar(); |
| 102 |
d->searchBar->setEditor(editor); |
| 103 |
|
| 104 |
// clang-format off |
| 105 |
// update editor actions on request |
| 106 |
Gui::MainWindow* mw = Gui::getMainWindow(); |
| 107 |
connect(editor, &QPlainTextEdit::undoAvailable, mw, &MainWindow::updateEditorActions); |
| 108 |
connect(editor, &QPlainTextEdit::redoAvailable, mw, &MainWindow::updateEditorActions); |
| 109 |
connect(editor, &QPlainTextEdit::copyAvailable, mw, &MainWindow::updateEditorActions); |
| 110 |
|
| 111 |
connect(editor, &TextEdit::showSearchBar, d->searchBar, &SearchBar::activate); |
| 112 |
connect(editor, &TextEdit::findNext, d->searchBar, &SearchBar::findNext); |
| 113 |
connect(editor, &TextEdit::findPrevious, d->searchBar, &SearchBar::findPrevious); |
| 114 |
// clang-format on |
| 115 |
|
| 116 |
// Create the layout containing the workspace and a tab bar |
| 117 |
auto hbox = new QFrame(this); |
| 118 |
hbox->setFrameShape(QFrame::StyledPanel); |
| 119 |
hbox->setFrameShadow(QFrame::Sunken); |
| 120 |
auto layout = new QVBoxLayout(); |
| 121 |
layout->setContentsMargins(1, 1, 1, 1); |
| 122 |
layout->addWidget(d->textEdit); |
| 123 |
layout->addWidget(d->searchBar); |
| 124 |
d->textEdit->setParent(hbox); |
| 125 |
d->searchBar->setParent(hbox); |
| 126 |
hbox->setLayout(layout); |
| 127 |
setCentralWidget(hbox); |
| 128 |
|
| 129 |
setCurrentFileName(QString()); |
| 130 |
d->textEdit->setFocus(); |
| 131 |
|
| 132 |
setWindowIcon(d->textEdit->windowIcon()); |
| 133 |
|
| 134 |
ParameterGrp::handle hPrefGrp = getWindowParameter(); |
| 135 |
hPrefGrp->Attach(this); |
| 136 |
hPrefGrp->NotifyAll(); |
| 137 |
|
| 138 |
d->activityTimer = new QTimer(this); |
| 139 |
// clang-format off |
| 140 |
connectionList << |
| 141 |
connect(d->activityTimer, &QTimer::timeout, |
| 142 |
this, &EditorView::checkTimestamp) << |
| 143 |
connect(d->textEdit->document(), &QTextDocument::modificationChanged, |
| 144 |
this, &EditorView::setWindowModified) << |
| 145 |
connect(d->textEdit->document(), &QTextDocument::undoAvailable, |
| 146 |
this, &EditorView::undoAvailable) << |
| 147 |
connect(d->textEdit->document(), &QTextDocument::redoAvailable, |
| 148 |
this, &EditorView::redoAvailable) << |
| 149 |
connect(d->textEdit->document(), &QTextDocument::contentsChange, |
| 150 |
this, &EditorView::contentsChange); |
| 151 |
// clang-format on |
| 152 |
} |
| 153 |
|
| 154 |
/** Destroys the object and frees any allocated resources */ |
| 155 |
EditorView::~EditorView() |
| 156 |
{ |
| 157 |
d->activityTimer->stop(); |
| 158 |
// to avoid the assert introduced a debug version of Qt >6.3. See QTBUG-105473 |
| 159 |
for (auto conn : connectionList) { // NOLINT(performance-for-range-copy) |
| 160 |
disconnect(conn); |
| 161 |
} |
| 162 |
delete d->activityTimer; |
| 163 |
delete d; |
| 164 |
getWindowParameter()->Detach(this); |
| 165 |
} |
| 166 |
|
| 167 |
QPlainTextEdit* EditorView::getEditor() const |
| 168 |
{ |
| 169 |
return d->textEdit; |
| 170 |
} |
| 171 |
|
| 172 |
void EditorView::showEvent(QShowEvent* event) |
| 173 |
{ |
| 174 |
Gui::MainWindow* mw = Gui::getMainWindow(); |
| 175 |
mw->updateEditorActions(); |
| 176 |
MDIView::showEvent(event); |
| 177 |
} |
| 178 |
|
| 179 |
void EditorView::hideEvent(QHideEvent* event) |
| 180 |
{ |
| 181 |
MDIView::hideEvent(event); |
| 182 |
} |
| 183 |
|
| 184 |
void EditorView::closeEvent(QCloseEvent* event) |
| 185 |
{ |
| 186 |
MDIView::closeEvent(event); |
| 187 |
if (event->isAccepted()) { |
| 188 |
d->aboutToClose = true; |
| 189 |
Gui::MainWindow* mw = Gui::getMainWindow(); |
| 190 |
mw->updateEditorActions(); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void EditorView::OnChange(Base::Subject<const char*>& rCaller, const char* rcReason) |
| 195 |
{ |
| 196 |
Q_UNUSED(rCaller); |
| 197 |
ParameterGrp::handle hPrefGrp = getWindowParameter(); |
| 198 |
if (strcmp(rcReason, "EnableLineNumber") == 0) { |
| 199 |
// bool show = hPrefGrp->GetBool( "EnableLineNumber", true ); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
void EditorView::checkTimestamp() |
| 204 |
{ |
| 205 |
QFileInfo fi(d->fileName); |
| 206 |
qint64 timeStamp = fi.lastModified().toSecsSinceEpoch(); |
| 207 |
if (timeStamp != d->timeStamp) { |
| 208 |
switch (QMessageBox::question( |
| 209 |
this, |
| 210 |
tr("Modified file"), |
| 211 |
tr("%1.\n\nThis has been modified outside of the source " |
| 212 |
"editor. Reload it?") |
| 213 |
.arg(d->fileName), |
| 214 |
QMessageBox::Yes | QMessageBox::No, |
| 215 |
QMessageBox::Yes |
| 216 |
)) { |
| 217 |
case QMessageBox::Yes: |
| 218 |
// updates time stamp and timer |
| 219 |
open(d->fileName); |
| 220 |
return; |
| 221 |
case QMessageBox::No: |
| 222 |
d->timeStamp = timeStamp; |
| 223 |
break; |
| 224 |
default: |
| 225 |
break; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
d->activityTimer->setSingleShot(true); |
| 230 |
d->activityTimer->start(3000); |
| 231 |
} |
| 232 |
|
| 233 |
/** |
| 234 |
* Runs the action specified by \a pMsg. |
| 235 |
*/ |
| 236 |
bool EditorView::onMsg(const char* pMsg, const char** /*ppReturn*/) |
| 237 |
{ |
| 238 |
// don't allow any actions if the editor is being closed |
| 239 |
if (d->aboutToClose) { |
| 240 |
return false; |
| 241 |
} |
| 242 |
|
| 243 |
if (strcmp(pMsg, "Save") == 0) { |
| 244 |
saveFile(); |
| 245 |
return true; |
| 246 |
} |
| 247 |
else if (strcmp(pMsg, "SaveAs") == 0) { |
| 248 |
saveAs(); |
| 249 |
return true; |
| 250 |
} |
| 251 |
else if (strcmp(pMsg, "Cut") == 0) { |
| 252 |
cut(); |
| 253 |
return true; |
| 254 |
} |
| 255 |
else if (strcmp(pMsg, "Copy") == 0) { |
| 256 |
copy(); |
| 257 |
return true; |
| 258 |
} |
| 259 |
else if (strcmp(pMsg, "Paste") == 0) { |
| 260 |
paste(); |
| 261 |
return true; |
| 262 |
} |
| 263 |
else if (strcmp(pMsg, "Undo") == 0) { |
| 264 |
undo(); |
| 265 |
return true; |
| 266 |
} |
| 267 |
else if (strcmp(pMsg, "Redo") == 0) { |
| 268 |
redo(); |
| 269 |
return true; |
| 270 |
} |
| 271 |
else if (strcmp(pMsg, "ViewFit") == 0) { |
| 272 |
// just ignore this |
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
return false; |
| 277 |
} |
| 278 |
|
| 279 |
/** |
| 280 |
* Checks if the action \a pMsg is available. This is for enabling/disabling |
| 281 |
* the corresponding buttons or menu items for this action. |
| 282 |
*/ |
| 283 |
bool EditorView::onHasMsg(const char* pMsg) const |
| 284 |
{ |
| 285 |
// don't allow any actions if the editor is being closed |
| 286 |
if (d->aboutToClose) { |
| 287 |
return false; |
| 288 |
} |
| 289 |
if (strcmp(pMsg, "Run") == 0) { |
| 290 |
return true; |
| 291 |
} |
| 292 |
if (strcmp(pMsg, "DebugStart") == 0) { |
| 293 |
return true; |
| 294 |
} |
| 295 |
if (strcmp(pMsg, "DebugStop") == 0) { |
| 296 |
return true; |
| 297 |
} |
| 298 |
if (strcmp(pMsg, "SaveAs") == 0) { |
| 299 |
return true; |
| 300 |
} |
| 301 |
if (strcmp(pMsg, "Print") == 0) { |
| 302 |
return true; |
| 303 |
} |
| 304 |
if (strcmp(pMsg, "PrintPreview") == 0) { |
| 305 |
return true; |
| 306 |
} |
| 307 |
if (strcmp(pMsg, "PrintPdf") == 0) { |
| 308 |
return true; |
| 309 |
} |
| 310 |
if (strcmp(pMsg, "Save") == 0) { |
| 311 |
return d->textEdit->document()->isModified(); |
| 312 |
} |
| 313 |
else if (strcmp(pMsg, "Cut") == 0) { |
| 314 |
bool canWrite = !d->textEdit->isReadOnly(); |
| 315 |
return (canWrite && (d->textEdit->textCursor().hasSelection())); |
| 316 |
} |
| 317 |
else if (strcmp(pMsg, "Copy") == 0) { |
| 318 |
return (d->textEdit->textCursor().hasSelection()); |
| 319 |
} |
| 320 |
else if (strcmp(pMsg, "Paste") == 0) { |
| 321 |
QClipboard* cb = QApplication::clipboard(); |
| 322 |
QString text; |
| 323 |
|
| 324 |
// Copy text from the clipboard (paste) |
| 325 |
text = cb->text(); |
| 326 |
|
| 327 |
bool canWrite = !d->textEdit->isReadOnly(); |
| 328 |
return (!text.isEmpty() && canWrite); |
| 329 |
} |
| 330 |
else if (strcmp(pMsg, "Undo") == 0) { |
| 331 |
return d->textEdit->document()->isUndoAvailable(); |
| 332 |
} |
| 333 |
else if (strcmp(pMsg, "Redo") == 0) { |
| 334 |
return d->textEdit->document()->isRedoAvailable(); |
| 335 |
} |
| 336 |
|
| 337 |
return false; |
| 338 |
} |
| 339 |
|
| 340 |
/** Checking on close state. */ |
| 341 |
bool EditorView::canClose() |
| 342 |
{ |
| 343 |
if (!d->textEdit->document()->isModified()) { |
| 344 |
return true; |
| 345 |
} |
| 346 |
this->setFocus(); // raises the view to front |
| 347 |
switch (QMessageBox::question( |
| 348 |
this, |
| 349 |
tr("Unsaved document"), |
| 350 |
tr("The document has been modified.\n" |
| 351 |
"Save all changes?"), |
| 352 |
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, |
| 353 |
QMessageBox::Cancel |
| 354 |
)) { |
| 355 |
case QMessageBox::Yes: |
| 356 |
return saveFile(); |
| 357 |
case QMessageBox::No: |
| 358 |
return true; |
| 359 |
case QMessageBox::Cancel: |
| 360 |
return false; |
| 361 |
default: |
| 362 |
return false; |
| 363 |
} |
| 364 |
} |
| 365 |
|
| 366 |
void EditorView::setDisplayName(EditorView::DisplayName type) |
| 367 |
{ |
| 368 |
d->displayName = type; |
| 369 |
} |
| 370 |
|
| 371 |
/** |
| 372 |
* Saves the content of the editor to a file specified by the appearing file dialog. |
| 373 |
*/ |
| 374 |
bool EditorView::saveAs() |
| 375 |
{ |
| 376 |
QString fn = FileDialog::getSaveFileName( |
| 377 |
this, |
| 378 |
QObject::tr("Save Macro"), |
| 379 |
QString(), |
| 380 |
QStringLiteral("%1 (*.FCMacro);;Python (*.py)").arg(tr("FreeCAD macro")) |
| 381 |
); |
| 382 |
if (fn.isEmpty()) { |
| 383 |
return false; |
| 384 |
} |
| 385 |
setCurrentFileName(fn); |
| 386 |
return saveFile(); |
| 387 |
} |
| 388 |
|
| 389 |
/** |
| 390 |
* Opens the file \a fileName. |
| 391 |
*/ |
| 392 |
bool EditorView::open(const QString& fileName) |
| 393 |
{ |
| 394 |
if (!QFile::exists(fileName)) { |
| 395 |
return false; |
| 396 |
} |
| 397 |
QFile file(fileName); |
| 398 |
if (!file.open(QFile::ReadOnly)) { |
| 399 |
return false; |
| 400 |
} |
| 401 |
|
| 402 |
d->lock = true; |
| 403 |
d->textEdit->setPlainText(QString::fromUtf8(file.readAll())); |
| 404 |
d->lock = false; |
| 405 |
d->undos.clear(); |
| 406 |
d->redos.clear(); |
| 407 |
file.close(); |
| 408 |
|
| 409 |
QFileInfo fi(fileName); |
| 410 |
d->timeStamp = fi.lastModified().toSecsSinceEpoch(); |
| 411 |
d->activityTimer->setSingleShot(true); |
| 412 |
d->activityTimer->start(3000); |
| 413 |
|
| 414 |
setCurrentFileName(fileName); |
| 415 |
return true; |
| 416 |
} |
| 417 |
|
| 418 |
/** |
| 419 |
* Copies the selected text to the clipboard and deletes it from the text edit. |
| 420 |
* If there is no selected text nothing happens. |
| 421 |
*/ |
| 422 |
void EditorView::cut() |
| 423 |
{ |
| 424 |
d->textEdit->cut(); |
| 425 |
} |
| 426 |
|
| 427 |
/** |
| 428 |
* Copies any selected text to the clipboard. |
| 429 |
*/ |
| 430 |
void EditorView::copy() |
| 431 |
{ |
| 432 |
d->textEdit->copy(); |
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Pastes the text from the clipboard into the text edit at the current cursor position. |
| 437 |
* If there is no text in the clipboard nothing happens. |
| 438 |
*/ |
| 439 |
void EditorView::paste() |
| 440 |
{ |
| 441 |
d->textEdit->paste(); |
| 442 |
} |
| 443 |
|
| 444 |
/** |
| 445 |
* Undoes the last operation. |
| 446 |
* If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing |
| 447 |
* happens. |
| 448 |
*/ |
| 449 |
void EditorView::undo() |
| 450 |
{ |
| 451 |
d->lock = true; |
| 452 |
if (!d->undos.isEmpty()) { |
| 453 |
d->redos << d->undos.back(); |
| 454 |
d->undos.pop_back(); |
| 455 |
} |
| 456 |
d->textEdit->document()->undo(); |
| 457 |
d->lock = false; |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Redoes the last operation. |
| 462 |
* If there is no operation to undo, i.e. there is no undo step in the undo/redo history, nothing |
| 463 |
* happens. |
| 464 |
*/ |
| 465 |
void EditorView::redo() |
| 466 |
{ |
| 467 |
d->lock = true; |
| 468 |
if (!d->redos.isEmpty()) { |
| 469 |
d->undos << d->redos.back(); |
| 470 |
d->redos.pop_back(); |
| 471 |
} |
| 472 |
d->textEdit->document()->redo(); |
| 473 |
d->lock = false; |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* Shows the printer dialog. |
| 478 |
*/ |
| 479 |
void EditorView::print() |
| 480 |
{ |
| 481 |
QPrinter printer(QPrinter::ScreenResolution); |
| 482 |
printer.setFullPage(true); |
| 483 |
QPrintDialog dlg(&printer, this); |
| 484 |
if (dlg.exec() == QDialog::Accepted) { |
| 485 |
d->textEdit->document()->print(&printer); |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
void EditorView::printPreview() |
| 490 |
{ |
| 491 |
QPrinter printer(QPrinter::ScreenResolution); |
| 492 |
QPrintPreviewDialog dlg(&printer, this); |
| 493 |
connect(&dlg, &QPrintPreviewDialog::paintRequested, this, qOverload<QPrinter*>(&EditorView::print)); |
| 494 |
dlg.exec(); |
| 495 |
} |
| 496 |
|
| 497 |
void EditorView::print(QPrinter* printer) |
| 498 |
{ |
| 499 |
d->textEdit->document()->print(printer); |
| 500 |
} |
| 501 |
|
| 502 |
/** |
| 503 |
* Prints the document into a Pdf file. |
| 504 |
*/ |
| 505 |
void EditorView::printPdf() |
| 506 |
{ |
| 507 |
QString filename = FileDialog::getSaveFileName( |
| 508 |
this, |
| 509 |
tr("Export PDF"), |
| 510 |
QString(), |
| 511 |
QStringLiteral("%1 (*.pdf)").arg(tr("PDF file")) |
| 512 |
); |
| 513 |
if (!filename.isEmpty()) { |
| 514 |
QPrinter printer(QPrinter::ScreenResolution); |
| 515 |
// setPdfVersion sets the printed PDF Version to what is chosen in |
| 516 |
// Preferences/Import-Export/PDF more details under: |
| 517 |
// https://www.kdab.com/creating-pdfa-documents-qt/ |
| 518 |
printer.setPdfVersion(Gui::Dialog::DlgSettingsPDF::evaluatePDFVersion()); |
| 519 |
printer.setOutputFormat(QPrinter::PdfFormat); |
| 520 |
printer.setOutputFileName(filename); |
| 521 |
printer.setCreator(QString::fromStdString(App::Application::getNameWithVersion())); |
| 522 |
d->textEdit->document()->print(&printer); |
| 523 |
} |
| 524 |
} |
| 525 |
|
| 526 |
void EditorView::setCurrentFileName(const QString& fileName) |
| 527 |
{ |
| 528 |
d->fileName = fileName; |
| 529 |
Q_EMIT changeFileName(d->fileName); |
| 530 |
d->textEdit->document()->setModified(false); |
| 531 |
|
| 532 |
QString name; |
| 533 |
QFileInfo fi(fileName); |
| 534 |
switch (d->displayName) { |
| 535 |
case FullName: |
| 536 |
name = fileName; |
| 537 |
break; |
| 538 |
case FileName: |
| 539 |
name = fi.fileName(); |
| 540 |
break; |
| 541 |
case BaseName: |
| 542 |
name = fi.baseName(); |
| 543 |
break; |
| 544 |
} |
| 545 |
|
| 546 |
QString shownName; |
| 547 |
if (fileName.isEmpty()) { |
| 548 |
shownName = tr("untitled[*]"); |
| 549 |
} |
| 550 |
else { |
| 551 |
shownName = QStringLiteral("%1[*]").arg(name); |
| 552 |
} |
| 553 |
shownName += tr(" - Editor"); |
| 554 |
setWindowTitle(shownName); |
| 555 |
setWindowModified(false); |
| 556 |
} |
| 557 |
|
| 558 |
QString EditorView::fileName() const |
| 559 |
{ |
| 560 |
return d->fileName; |
| 561 |
} |
| 562 |
|
| 563 |
/** |
| 564 |
* Saves the contents to a file. |
| 565 |
*/ |
| 566 |
bool EditorView::saveFile() |
| 567 |
{ |
| 568 |
if (d->fileName.isEmpty()) { |
| 569 |
return saveAs(); |
| 570 |
} |
| 571 |
|
| 572 |
QFile file(d->fileName); |
| 573 |
if (!file.open(QFile::WriteOnly)) { |
| 574 |
return false; |
| 575 |
} |
| 576 |
QTextStream ts(&file); |
| 577 |
#if QT_VERSION < 0x060000 |
| 578 |
ts.setCodec("UTF-8"); |
| 579 |
#endif |
| 580 |
ts << d->textEdit->document()->toPlainText(); |
| 581 |
file.close(); |
| 582 |
d->textEdit->document()->setModified(false); |
| 583 |
|
| 584 |
QFileInfo fi(d->fileName); |
| 585 |
d->timeStamp = fi.lastModified().toSecsSinceEpoch(); |
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
void EditorView::undoAvailable(bool undo) |
| 590 |
{ |
| 591 |
if (!undo) { |
| 592 |
d->undos.clear(); |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
void EditorView::redoAvailable(bool redo) |
| 597 |
{ |
| 598 |
if (!redo) { |
| 599 |
d->redos.clear(); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
void EditorView::contentsChange(int position, int charsRemoved, int charsAdded) |
| 604 |
{ |
| 605 |
Q_UNUSED(position); |
| 606 |
if (d->lock) { |
| 607 |
return; |
| 608 |
} |
| 609 |
if (charsRemoved > 0 && charsAdded > 0) { |
| 610 |
return; // syntax highlighting |
| 611 |
} |
| 612 |
else if (charsRemoved > 0) { |
| 613 |
d->undos << tr("%1 chars removed").arg(charsRemoved); |
| 614 |
} |
| 615 |
else if (charsAdded > 0) { |
| 616 |
d->undos << tr("%1 chars added").arg(charsAdded); |
| 617 |
} |
| 618 |
else { |
| 619 |
d->undos << tr("Formatted"); |
| 620 |
} |
| 621 |
d->redos.clear(); |
| 622 |
} |
| 623 |
|
| 624 |
/** |
| 625 |
* Get the undo history. |
| 626 |
*/ |
| 627 |
QStringList EditorView::undoActions() const |
| 628 |
{ |
| 629 |
return d->undos; |
| 630 |
} |
| 631 |
|
| 632 |
/** |
| 633 |
* Get the redo history. |
| 634 |
*/ |
| 635 |
QStringList EditorView::redoActions() const |
| 636 |
{ |
| 637 |
return d->redos; |
| 638 |
; |
| 639 |
} |
| 640 |
|
| 641 |
void EditorView::focusInEvent(QFocusEvent*) |
| 642 |
{ |
| 643 |
d->textEdit->setFocus(); |
| 644 |
} |
| 645 |
|
| 646 |
// --------------------------------------------------------- |
| 647 |
|
| 648 |
TYPESYSTEM_SOURCE_ABSTRACT(Gui::PythonEditorView, Gui::EditorView) |
| 649 |
|
| 650 |
PythonEditorView::PythonEditorView(PythonEditor* editor, QWidget* parent) |
| 651 |
: EditorView(editor, parent) |
| 652 |
, _pye(editor) |
| 653 |
{ |
| 654 |
connect(this, &PythonEditorView::changeFileName, editor, &PythonEditor::setFileName); |
| 655 |
watcher = new PythonTracingWatcher(this); |
| 656 |
} |
| 657 |
|
| 658 |
PythonEditorView::~PythonEditorView() |
| 659 |
{ |
| 660 |
delete watcher; |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Runs the action specified by \a pMsg. |
| 665 |
*/ |
| 666 |
bool PythonEditorView::onMsg(const char* pMsg, const char** ppReturn) |
| 667 |
{ |
| 668 |
if (strcmp(pMsg, "Run") == 0) { |
| 669 |
executeScript(); |
| 670 |
return true; |
| 671 |
} |
| 672 |
else if (strcmp(pMsg, "StartDebug") == 0) { |
| 673 |
QTimer::singleShot(300, this, &PythonEditorView::startDebug); |
| 674 |
return true; |
| 675 |
} |
| 676 |
else if (strcmp(pMsg, "ToggleBreakpoint") == 0) { |
| 677 |
toggleBreakpoint(); |
| 678 |
return true; |
| 679 |
} |
| 680 |
return EditorView::onMsg(pMsg, ppReturn); |
| 681 |
} |
| 682 |
|
| 683 |
/** |
| 684 |
* Checks if the action \a pMsg is available. This is for enabling/disabling |
| 685 |
* the corresponding buttons or menu items for this action. |
| 686 |
*/ |
| 687 |
bool PythonEditorView::onHasMsg(const char* pMsg) const |
| 688 |
{ |
| 689 |
if (strcmp(pMsg, "Run") == 0) { |
| 690 |
return true; |
| 691 |
} |
| 692 |
if (strcmp(pMsg, "StartDebug") == 0) { |
| 693 |
return true; |
| 694 |
} |
| 695 |
if (strcmp(pMsg, "ToggleBreakpoint") == 0) { |
| 696 |
return true; |
| 697 |
} |
| 698 |
return EditorView::onHasMsg(pMsg); |
| 699 |
} |
| 700 |
|
| 701 |
/** |
| 702 |
* Runs the opened script in the macro manager. |
| 703 |
*/ |
| 704 |
void PythonEditorView::executeScript() |
| 705 |
{ |
| 706 |
// always save the macro when it is modified |
| 707 |
if (EditorView::onHasMsg("Save")) { |
| 708 |
EditorView::onMsg("Save", nullptr); |
| 709 |
} |
| 710 |
try { |
| 711 |
getMainWindow()->setCursor(Qt::WaitCursor); |
| 712 |
PythonTracingLocker tracelock(watcher->getTrace()); |
| 713 |
Application::Instance->macroManager()->run(Gui::MacroManager::File, fileName().toUtf8()); |
| 714 |
getMainWindow()->unsetCursor(); |
| 715 |
} |
| 716 |
catch (const Base::SystemExitException&) { |
| 717 |
// handle SystemExit exceptions |
| 718 |
Base::PyGILStateLocker locker; |
| 719 |
Base::PyException e; |
| 720 |
e.reportException(); |
| 721 |
getMainWindow()->unsetCursor(); |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
void PythonEditorView::startDebug() |
| 726 |
{ |
| 727 |
_pye->startDebug(); |
| 728 |
} |
| 729 |
|
| 730 |
void PythonEditorView::toggleBreakpoint() |
| 731 |
{ |
| 732 |
_pye->toggleBreakpoint(); |
| 733 |
} |
| 734 |
|
| 735 |
void PythonEditorView::showDebugMarker(int line) |
| 736 |
{ |
| 737 |
_pye->showDebugMarker(line); |
| 738 |
} |
| 739 |
|
| 740 |
void PythonEditorView::hideDebugMarker() |
| 741 |
{ |
| 742 |
_pye->hideDebugMarker(); |
| 743 |
} |
| 744 |
|
| 745 |
// ---------------------------------------------------------------------------- |
| 746 |
|
| 747 |
SearchBar::SearchBar(QWidget* parent) |
| 748 |
: QWidget(parent) |
| 749 |
, textEditor(nullptr) |
| 750 |
{ |
| 751 |
horizontalLayout = new QHBoxLayout(this); |
| 752 |
horizontalLayout->setSpacing(3); |
| 753 |
|
| 754 |
closeButton = new QToolButton(this); |
| 755 |
closeButton->setIcon(style()->standardIcon(QStyle::SP_DialogCloseButton)); |
| 756 |
closeButton->setAutoRaise(true); |
| 757 |
connect(closeButton, &QToolButton::clicked, this, &SearchBar::deactivate); |
| 758 |
|
| 759 |
horizontalLayout->addWidget(closeButton); |
| 760 |
|
| 761 |
searchText = new QLineEdit(this); |
| 762 |
searchText->setClearButtonEnabled(true); |
| 763 |
horizontalLayout->addWidget(searchText); |
| 764 |
connect(searchText, &QLineEdit::returnPressed, this, &SearchBar::findNext); |
| 765 |
connect(searchText, &QLineEdit::textChanged, this, &SearchBar::findCurrent); |
| 766 |
connect(searchText, &QLineEdit::textChanged, this, &SearchBar::updateButtons); |
| 767 |
|
| 768 |
prevButton = new QToolButton(this); |
| 769 |
prevButton->setIcon(style()->standardIcon(QStyle::SP_ArrowBack)); |
| 770 |
prevButton->setAutoRaise(true); |
| 771 |
prevButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 772 |
horizontalLayout->addWidget(prevButton); |
| 773 |
connect(prevButton, &QToolButton::clicked, this, &SearchBar::findPrevious); |
| 774 |
|
| 775 |
nextButton = new QToolButton(this); |
| 776 |
nextButton->setIcon(style()->standardIcon(QStyle::SP_ArrowForward)); |
| 777 |
nextButton->setAutoRaise(true); |
| 778 |
nextButton->setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
| 779 |
horizontalLayout->addWidget(nextButton); |
| 780 |
connect(nextButton, &QToolButton::clicked, this, &SearchBar::findNext); |
| 781 |
|
| 782 |
matchCase = new QCheckBox(this); |
| 783 |
horizontalLayout->addWidget(matchCase); |
| 784 |
connect(matchCase, &QCheckBox::toggled, this, &SearchBar::findCurrent); |
| 785 |
|
| 786 |
matchWord = new QCheckBox(this); |
| 787 |
horizontalLayout->addWidget(matchWord); |
| 788 |
connect(matchWord, &QCheckBox::toggled, this, &SearchBar::findCurrent); |
| 789 |
|
| 790 |
horizontalSpacer = new QSpacerItem(192, 20, QSizePolicy::Expanding, QSizePolicy::Minimum); |
| 791 |
horizontalLayout->addItem(horizontalSpacer); |
| 792 |
|
| 793 |
retranslateUi(); |
| 794 |
|
| 795 |
setMinimumWidth(minimumSizeHint().width()); |
| 796 |
updateButtons(); |
| 797 |
hide(); |
| 798 |
} |
| 799 |
|
| 800 |
void SearchBar::setEditor(QPlainTextEdit* textEdit) |
| 801 |
{ |
| 802 |
textEditor = textEdit; |
| 803 |
} |
| 804 |
|
| 805 |
void SearchBar::keyPressEvent(QKeyEvent* event) |
| 806 |
{ |
| 807 |
if (event->key() == Qt::Key_Escape) { |
| 808 |
hide(); |
| 809 |
return; |
| 810 |
} |
| 811 |
|
| 812 |
QWidget::keyPressEvent(event); |
| 813 |
} |
| 814 |
|
| 815 |
void SearchBar::retranslateUi() |
| 816 |
{ |
| 817 |
prevButton->setText(tr("Previous")); |
| 818 |
nextButton->setText(tr("Next")); |
| 819 |
matchCase->setText(tr("Case sensitive")); |
| 820 |
matchWord->setText(tr("Whole words")); |
| 821 |
} |
| 822 |
|
| 823 |
void SearchBar::activate() |
| 824 |
{ |
| 825 |
show(); |
| 826 |
searchText->selectAll(); |
| 827 |
searchText->setFocus(Qt::ShortcutFocusReason); |
| 828 |
} |
| 829 |
|
| 830 |
void SearchBar::deactivate() |
| 831 |
{ |
| 832 |
if (textEditor) { |
| 833 |
textEditor->setFocus(); |
| 834 |
} |
| 835 |
hide(); |
| 836 |
} |
| 837 |
|
| 838 |
void SearchBar::findPrevious() |
| 839 |
{ |
| 840 |
findText(true, false, searchText->text()); |
| 841 |
} |
| 842 |
|
| 843 |
void SearchBar::findNext() |
| 844 |
{ |
| 845 |
findText(true, true, searchText->text()); |
| 846 |
} |
| 847 |
|
| 848 |
void SearchBar::findCurrent() |
| 849 |
{ |
| 850 |
findText(false, true, searchText->text()); |
| 851 |
} |
| 852 |
|
| 853 |
void SearchBar::findText(bool skip, bool next, const QString& str) |
| 854 |
{ |
| 855 |
if (!textEditor) { |
| 856 |
return; |
| 857 |
} |
| 858 |
|
| 859 |
QTextCursor cursor = textEditor->textCursor(); |
| 860 |
QTextDocument* doc = textEditor->document(); |
| 861 |
if (!doc || cursor.isNull()) { |
| 862 |
return; |
| 863 |
} |
| 864 |
|
| 865 |
if (cursor.hasSelection()) { |
| 866 |
cursor.setPosition((skip && next) ? cursor.position() : cursor.anchor()); |
| 867 |
} |
| 868 |
|
| 869 |
bool found = true; |
| 870 |
QTextCursor newCursor = cursor; |
| 871 |
if (!str.isEmpty()) { |
| 872 |
QTextDocument::FindFlags options; |
| 873 |
if (!next) { |
| 874 |
options |= QTextDocument::FindBackward; |
| 875 |
} |
| 876 |
if (matchCase->isChecked()) { |
| 877 |
options |= QTextDocument::FindCaseSensitively; |
| 878 |
} |
| 879 |
if (matchWord->isChecked()) { |
| 880 |
options |= QTextDocument::FindWholeWords; |
| 881 |
} |
| 882 |
|
| 883 |
newCursor = doc->find(str, cursor, options); |
| 884 |
if (newCursor.isNull()) { |
| 885 |
QTextCursor ac(doc); |
| 886 |
ac.movePosition( |
| 887 |
options & QTextDocument::FindBackward ? QTextCursor::End : QTextCursor::Start |
| 888 |
); |
| 889 |
newCursor = doc->find(str, ac, options); |
| 890 |
if (newCursor.isNull()) { |
| 891 |
found = false; |
| 892 |
newCursor = cursor; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
if (!isVisible()) { |
| 898 |
show(); |
| 899 |
} |
| 900 |
|
| 901 |
textEditor->setTextCursor(newCursor); |
| 902 |
|
| 903 |
QString styleSheet; |
| 904 |
if (!found) { |
| 905 |
styleSheet = QStringLiteral( |
| 906 |
" QLineEdit {\n" |
| 907 |
" background-color: rgb(221,144,161);\n" |
| 908 |
" }\n" |
| 909 |
); |
| 910 |
} |
| 911 |
|
| 912 |
searchText->setStyleSheet(styleSheet); |
| 913 |
} |
| 914 |
|
| 915 |
void SearchBar::updateButtons() |
| 916 |
{ |
| 917 |
bool empty = searchText->text().isEmpty(); |
| 918 |
prevButton->setDisabled(empty); |
| 919 |
nextButton->setDisabled(empty); |
| 920 |
} |
| 921 |
|
| 922 |
void SearchBar::changeEvent(QEvent* event) |
| 923 |
{ |
| 924 |
if (event->type() == QEvent::LanguageChange) { |
| 925 |
retranslateUi(); |
| 926 |
} |
| 927 |
|
| 928 |
QWidget::changeEvent(event); |
| 929 |
} |
| 930 |
|
| 931 |
#include "moc_EditorView.cpp" |