Skip to content

Commit a6fde9b

Browse files
Fix saving no-qrs tabs (#792)
* Little refactoring * Fix closing script-tab * Hot fix for qscintilla signal about textChanged * Lupdate translations
1 parent badca37 commit a6fde9b

File tree

11 files changed

+73
-68
lines changed

11 files changed

+73
-68
lines changed

plugins/robots/interpreters/interpreterCore/src/robotsPluginFacade.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,9 @@ void RobotsPluginFacade::init(const qReal::PluginConfigurator &configurer)
226226
out() << code;
227227
out.flush();
228228
const qReal::Id activeDiagram = mMainWindow->activeDiagram();
229-
mTextManager->showInTextEditor(codePath, qReal::text::Languages::pickByExtension(codePath.suffix()));
229+
// QString() need to use method with binding
230+
// TODO: need refactoring showInTextEditor(?)
231+
mTextManager->showInTextEditor(codePath, QString(), qReal::text::Languages::pickByExtension(codePath.suffix()));
230232
if (!activeDiagram.isNull()
231233
&& mRobotModelManager.model().friendlyName().contains("2d", Qt::CaseInsensitive)) {
232234
mMainWindow->activateItemOrDiagram(activeDiagram);

qrgui/mainWindow/mainWindow.cpp

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -896,16 +896,11 @@ void MainWindow::closeCurrentTab()
896896

897897
void MainWindow::closeTab(int index)
898898
{
899+
switchToTab(index);
899900
QWidget * const widget = mUi->tabs->widget(index);
900-
EditorView * const diagram = dynamic_cast<EditorView *>(widget);
901-
StartWidget * const start = dynamic_cast<StartWidget *>(widget);
902-
text::QScintillaTextEdit * const possibleCodeTab = dynamic_cast<text::QScintillaTextEdit *>(widget);
903-
gestures::GesturesWidget * const gesture = dynamic_cast<gestures::GesturesWidget *>(widget);
904901
bool isClosed = false;
905902

906-
const QString path = mTextManager->path(possibleCodeTab);
907-
908-
if (diagram) {
903+
if (auto const diagram = dynamic_cast<EditorView *>(widget)) {
909904
const Id diagramId = diagram->editorViewScene().rootItemId();
910905
if (diagramId.type() == qReal::Id("RobotsMetamodel", "RobotsDiagram", "RobotsDiagramNode")) {
911906
isClosed = mProjectManager->suggestToSaveChangesOrCancel();
@@ -917,12 +912,14 @@ void MainWindow::closeTab(int index)
917912
mController->moduleClosed(diagramId.toString());
918913
emit mFacade->events().diagramClosed(diagramId);
919914
}
920-
} else if (start || gesture) {
915+
} else if (auto const start = dynamic_cast<StartWidget *>(widget)) {
921916
isClosed = true;
922-
} else if (possibleCodeTab) {
923-
isClosed = mTextManager->unbindCode(possibleCodeTab);
924-
if (isClosed) {
925-
emit mFacade->events().codeTabClosed(QFileInfo(path));
917+
} else if (auto const gesture = dynamic_cast<gestures::GesturesWidget *>(widget)) {
918+
isClosed = true;
919+
} else if (auto const possibleCodeTab = dynamic_cast<text::QScintillaTextEdit *>(widget)) {
920+
if (mTextManager->suggestToSaveCode(possibleCodeTab) && mTextManager->unbindCode(possibleCodeTab)) {
921+
isClosed = true;
922+
emit mFacade->events().codeTabClosed(QFileInfo(mTextManager->path(possibleCodeTab)));
926923
}
927924
} else {
928925
QLOG_ERROR() << "Unknown type of tab " << widget->objectName();

qrgui/textEditor/qscintillaTextEdit.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ void QScintillaTextEdit::find()
153153
void QScintillaTextEdit::init()
154154
{
155155
// For some reason c++11-style connections do not work here!
156-
connect(this, &QScintillaTextEdit::textChanged, this, &QScintillaTextEdit::emitTextWasModified);
156+
// TODO: fix with new SIGNAL/SLOT signature
157+
connect(this, SIGNAL(textChanged()), this, SLOT(emitTextWasModified()));
157158
initFindModeConnections();
158159
setDefaultSettings();
159160
setCurrentLanguage(Languages::textFileInfo("*.txt"));

qrgui/textEditor/textManager.cpp

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,8 @@ bool TextManager::unbindCode(const QString &filePath)
8383
return mDiagramCodeManager.remove(mDiagramCodeManager.key(filePath), filePath) != 0;
8484
}
8585

86-
bool TextManager::unbindCode(text::QScintillaTextEdit *code)
86+
bool TextManager::suggestToSaveCode(text::QScintillaTextEdit *code)
8787
{
88-
QString str = mPath.value(code);
89-
if (mDiagramCodeManager.key(mPath.value(code)) == Id()) {
90-
// Unbind is successful because this code doesn't bind to any diagram
91-
return true;
92-
}
9388
if (mModified[mPath.value(code)].second) {
9489
switch (utils::QRealMessageBox::question(
9590
mMainWindow.currentTab()
@@ -99,14 +94,28 @@ bool TextManager::unbindCode(text::QScintillaTextEdit *code)
9994
{
10095
case QMessageBox::Yes:
10196
saveText(false);
102-
break;
97+
return true;
10398
case QMessageBox::No:
104-
break;
99+
{
100+
// very bad way but now I have only this idea
101+
// need to reset code to last saved state
102+
setModified(code, false);
103+
return true;
104+
}
105105
default:
106106
return false;
107107
}
108108
}
109-
return unbindCode(mPath.value(code));
109+
return true;
110+
}
111+
112+
bool TextManager::unbindCode(text::QScintillaTextEdit *code)
113+
{
114+
if (mDiagramCodeManager.key(mPath.value(code)) == Id()) {
115+
// Unbind is successful because this code doesn't bind to any diagram
116+
return true;
117+
}
118+
return suggestToSaveCode(code) && unbindCode(mPath.value(code));
110119
}
111120

112121
bool TextManager::closeFile(const QString &filePath)
@@ -267,27 +276,21 @@ bool TextManager::saveText(bool saveAs)
267276
return false;
268277
}
269278

270-
const Id diagram = TextManager::diagram(area);
271-
QFileInfo fileInfo;
272-
const QString filepath = path(area);
273-
const bool defaultPath = isDefaultPath(filepath);
274-
275-
QString editorExtension = QString("%1 (*.%2)").arg(
276-
area->currentLanguage().extensionDescription
277-
, area->currentLanguage().extension);
278-
const QString extensionDescriptions = editorExtension + ";;" + tr("All files (*)");
279-
QString *currentExtensionDescription = &editorExtension;
279+
QFileInfo fileInfo = path(area);
280280

281-
if (saveAs) {
281+
if (saveAs || fileInfo.fileName().isEmpty()) {
282+
QString editorExtension = QString("%1 (*.%2)").arg(
283+
area->currentLanguage().extensionDescription
284+
, area->currentLanguage().extension);
285+
const QString extensionDescriptions = editorExtension + ";;" + tr("All files (*)");
286+
QString *currentExtensionDescription = &editorExtension;
282287
fileInfo = QFileInfo(utils::QRealFileDialog::getSaveFileName("SaveTextFromTextManager"
283288
, mMainWindow.windowWidget()
284289
, tr("Save generated code")
285290
, QString()
286291
, extensionDescriptions
287292
, QString()
288293
, currentExtensionDescription));
289-
} else {
290-
fileInfo = path(area);
291294
}
292295

293296
if (!fileInfo.fileName().isEmpty()) {
@@ -297,12 +300,13 @@ bool TextManager::saveText(bool saveAs)
297300

298301
out() << area->text();
299302

300-
if (defaultPath || saveAs) {
303+
if (isDefaultPath(path(area)) || saveAs) {
301304
changeFilePath(path(area), fileInfo.absoluteFilePath());
302305
}
303306

304307
setModified(area, false);
305308

309+
const Id diagram = TextManager::diagram(area);
306310
if (saveAs && !diagram.isNull()) {
307311
emit mSystemEvents.codePathChanged(diagram, path(area), fileInfo);
308312
}

qrgui/textEditor/textManager.h

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,42 +38,43 @@ class QRGUI_TEXT_EDITOR_EXPORT TextManager : public TextManagerInterface
3838
TextManager(SystemEvents &systemEvents, gui::MainWindowInterpretersInterface &mainWindow);
3939

4040
/// Reads code source file and create new QScintillaTextEdit associated with this file (rather with filepath)
41-
bool openFile(const QString &filePath, const QString &genName, const LanguageInfo &language);
41+
bool openFile(const QString &filePath, const QString &genName, const LanguageInfo &language) override;
4242

4343
/// Remove all info about filePath (including QScintillaTextEdit associated with it)
44-
bool closeFile(const QString &filePath);
44+
bool closeFile(const QString &filePath) override;
4545

46-
void changeFilePath(const QString &from, const QString &to);
46+
void changeFilePath(const QString &from, const QString &to) override;
4747

4848
/// Binds diagram with another code source file.
49-
bool bindCode(const Id &diagram, const QString &filePath);
50-
51-
bool unbindCode(const QString &filePath);
52-
bool unbindCode(text::QScintillaTextEdit *code);
53-
text::QScintillaTextEdit *code(const QString &filePath) const;
54-
QList<text::QScintillaTextEdit *> code(const Id &diagram) const;
55-
bool contains(const QString &filePath) const;
49+
bool bindCode(const Id &diagram, const QString &filePath) override;
50+
51+
bool unbindCode(const QString &filePath) override;
52+
bool unbindCode(text::QScintillaTextEdit *code) override;
53+
bool suggestToSaveCode(text::QScintillaTextEdit *code);
54+
text::QScintillaTextEdit *code(const QString &filePath) const override;
55+
QList<text::QScintillaTextEdit *> code(const Id &diagram) const override;
56+
bool contains(const QString &filePath) const override;
5657
bool removeDiagram(const Id &diagram);
57-
Id diagram(text::QScintillaTextEdit *code) const;
58-
QString path(text::QScintillaTextEdit *code) const;
59-
bool isDefaultPath(const QString &path) const;
60-
bool isModified(const QString &path) const;
61-
bool isModifiedEver(const QString &path) const;
58+
Id diagram(text::QScintillaTextEdit *code) const override;
59+
QString path(text::QScintillaTextEdit *code) const override;
60+
bool isDefaultPath(const QString &path) const override;
61+
bool isModified(const QString &path) const override;
62+
bool isModifiedEver(const QString &path) const override;
6263

6364
/// Opens new tab with file created by some generator in text editor and shows a text in it
6465
/// @param fileInfo A filepath to file with text
6566
/// @param genName A name of generator which created this file
66-
void showInTextEditor(const QFileInfo &fileInfo, const QString &genName, const LanguageInfo &language);
67+
void showInTextEditor(const QFileInfo &fileInfo, const QString &genName, const LanguageInfo &language) override;
6768

6869
/// Opens new tab with file
6970
/// @param fileInfo A filepath to file with text
70-
void showInTextEditor(const QFileInfo &fileInfo, const LanguageInfo &language);
71+
void showInTextEditor(const QFileInfo &fileInfo, const LanguageInfo &language) override;
7172

7273
/// Saves text from tab to another or same file
7374
/// @param saveAs Defines what to do: save to the same file or in another
74-
bool saveText(bool saveAs);
75+
bool saveText(bool saveAs) override;
7576

76-
QString generatorName(const QString &filepath) const;
77+
QString generatorName(const QString &filepath) const override;
7778
CodeBlockManager &codeBlockManager();
7879

7980
private slots:

qrtranslations/fr/plugins/robots/interpreterCore_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@
309309
<translation type="unfinished"></translation>
310310
</message>
311311
<message>
312-
<location line="+101"/>
312+
<location line="+103"/>
313313
<source>No saved code found in the qrs file</source>
314314
<translation type="unfinished"></translation>
315315
</message>

qrtranslations/fr/qrgui_mainWindow_fr.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ WARNING: The settings will be restored after application restart</source>
779779
<translation>Le chargement d&apos;une extention a échoué :</translation>
780780
</message>
781781
<message>
782-
<location line="+111"/>
782+
<location line="+108"/>
783783
<location line="+16"/>
784784
<location line="+23"/>
785785
<source>Shape Editor</source>

qrtranslations/fr/qrgui_textEditor_fr.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<context>
55
<name>qReal::text::TextManager</name>
66
<message>
7-
<location filename="../../qrgui/textEditor/textManager.cpp" line="+96"/>
7+
<location filename="../../qrgui/textEditor/textManager.cpp" line="+91"/>
88
<source>Confirmation</source>
99
<translation type="unfinished"></translation>
1010
</message>
@@ -14,12 +14,12 @@
1414
<translation type="unfinished"></translation>
1515
</message>
1616
<message>
17-
<location line="+181"/>
17+
<location line="+193"/>
1818
<source>All files (*)</source>
1919
<translation>Tous les fichiers (*)</translation>
2020
</message>
2121
<message>
22-
<location line="+6"/>
22+
<location line="+4"/>
2323
<source>Save generated code</source>
2424
<translation>Enregister le code généré</translation>
2525
</message>

qrtranslations/ru/plugins/robots/interpreterCore_ru.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@
223223
<translation>Экспорт упражнения по заданному пути не удался (попробуйте другой путь)</translation>
224224
</message>
225225
<message>
226-
<location line="+101"/>
226+
<location line="+103"/>
227227
<source>No saved code found in the qrs file</source>
228228
<translation>В qrs не найден сохраннёный код</translation>
229229
</message>

qrtranslations/ru/qrgui_mainWindow_ru.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,7 +781,7 @@
781781
<translation>Загрузка плагина завершилась ошибкой: </translation>
782782
</message>
783783
<message>
784-
<location line="+111"/>
784+
<location line="+108"/>
785785
<location line="+16"/>
786786
<location line="+23"/>
787787
<source>Shape Editor</source>
@@ -818,7 +818,7 @@
818818
<translation>Создать диаграмму</translation>
819819
</message>
820820
<message>
821-
<location line="-1030"/>
821+
<location line="-1027"/>
822822
<source>Restore default settings</source>
823823
<translation>Восстановить настройки по-умолчанию</translation>
824824
</message>
@@ -837,7 +837,7 @@ WARNING: The settings will be restored after application restart</source>
837837
<translation>Не удалось сохранить файл, попробуйте сохранить его в другое место</translation>
838838
</message>
839839
<message>
840-
<location line="+629"/>
840+
<location line="+626"/>
841841
<source>Undo</source>
842842
<translation>Отменить</translation>
843843
</message>

0 commit comments

Comments
 (0)