diff options
| author | isanae <14251494+isanae@users.noreply.github.com> | 2019-06-15 18:54:19 -0400 |
|---|---|---|
| committer | isanae <14251494+isanae@users.noreply.github.com> | 2019-07-02 10:10:16 -0400 |
| commit | 6bd5bed29f3ebcc1155e615d7daa1c9cd1724efb (patch) | |
| tree | 9dea3a3a0b50b181f49c99eb57cfc3ef4b7a7ecf | |
| parent | 59e0c4aa34fec9048d064705863c3269aacd86b5 (diff) | |
added initial toolbar, splitter
moved most of the text editor stuff to a new TextEditor class
| -rw-r--r-- | src/CMakeLists.txt | 3 | ||||
| -rw-r--r-- | src/modinfodialog.cpp | 77 | ||||
| -rw-r--r-- | src/modinfodialog.h | 10 | ||||
| -rw-r--r-- | src/modinfodialog.ui | 130 | ||||
| -rw-r--r-- | src/texteditor.cpp | 76 | ||||
| -rw-r--r-- | src/texteditor.h | 36 |
6 files changed, 249 insertions, 83 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f654f9b4..f75ab1e0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -119,6 +119,7 @@ SET(organizer_SRCS statusbar.cpp apiuseraccount.cpp filerenamer.cpp + texteditor.cpp shared/windows_error.cpp shared/error_report.cpp @@ -219,6 +220,7 @@ SET(organizer_HDRS statusbar.h apiuseraccount.h filerenamer.h + texteditor.h shared/windows_error.h shared/error_report.h @@ -414,6 +416,7 @@ set(widgets modidlineedit noeditdelegate qtgroupingproxy + texteditor viewmarkingscrollbar ) diff --git a/src/modinfodialog.cpp b/src/modinfodialog.cpp index f553a7be..db5b5fd0 100644 --- a/src/modinfodialog.cpp +++ b/src/modinfodialog.cpp @@ -36,6 +36,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include "pluginlistsortproxy.h" #include "previewgenerator.h" #include "previewdialog.h" +#include "texteditor.h" #include <QDir> #include <QDirIterator> @@ -358,6 +359,17 @@ ModInfoDialog::ModInfoDialog(ModInfo::Ptr modInfo, const DirectoryEntry *directo } } + m_textFileEditor.reset(new TextEditor(ui->textFileView)); + + connect( + m_textFileEditor.get(), &TextEditor::changed, + [&](bool b){ onTextFileChanged(b); }); + + ui->tabTextSplitter->setSizes({200, 1}); + ui->tabTextSplitter->setStretchFactor(0, 0); + ui->tabTextSplitter->setStretchFactor(1, 1); + setTextFileWordWrap(true); + // refresh everything but the conflict lists, which are done in exec() because // they depend on restoring the state to some widgets; this refresh has to be // done here because some of the checks below depend on the ui to decide which @@ -1030,9 +1042,12 @@ void ModInfoDialog::thumbnailClicked(const QString &fileName) bool ModInfoDialog::allowNavigateFromTXT() { - if (ui->saveTXTButton->isEnabled()) { - int res = QMessageBox::question(this, tr("Save changes?"), tr("Save changes to \"%1\"?").arg(ui->textFileView->property("currentFile").toString()), - QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + if (m_textFileEditor->dirty()) { + const int res = QMessageBox::question( + this, tr("Save changes?"), + tr("Save changes to \"%1\"?").arg(m_textFileEditor->filename()), + QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel); + if (res == QMessageBox::Cancel) { return false; } else if (res == QMessageBox::Yes) { @@ -1042,7 +1057,6 @@ bool ModInfoDialog::allowNavigateFromTXT() return true; } - bool ModInfoDialog::allowNavigateFromINI() { if (ui->saveButton->isEnabled()) { @@ -1057,14 +1071,14 @@ bool ModInfoDialog::allowNavigateFromINI() return true; } - -void ModInfoDialog::on_textFileList_currentItemChanged(QListWidgetItem *current, QListWidgetItem *previous) +void ModInfoDialog::on_textFileList_currentItemChanged( + QListWidgetItem *current, QListWidgetItem *previous) { - QString fullPath = m_RootPath + "/" + current->text(); + const QString fullPath = m_RootPath + "/" + current->text(); - QVariant currentFile = ui->textFileView->property("currentFile"); - if (currentFile.isValid() && (currentFile.toString() == fullPath)) { - // the new file is the same as the currently displayed file. May be the result of a cancelation + if (fullPath == m_textFileEditor->filename()) { + // the new file is the same as the currently displayed file. May be the + // result of a cancellation return; } @@ -1075,17 +1089,12 @@ void ModInfoDialog::on_textFileList_currentItemChanged(QListWidgetItem *current, } } - void ModInfoDialog::openTextFile(const QString &fileName) { - QString encoding; - ui->textFileView->setText(MOBase::readFileText(fileName, &encoding)); - ui->textFileView->setProperty("currentFile", fileName); - ui->textFileView->setProperty("encoding", encoding); - ui->saveTXTButton->setEnabled(false); + m_textFileEditor->load(fileName); + ui->textFileSave->setEnabled(false); } - void ModInfoDialog::openIniFile(const QString &fileName) { QFile iniFile(fileName); @@ -1155,35 +1164,31 @@ void ModInfoDialog::on_iniTweaksList_currentItemChanged(QListWidgetItem *current } - void ModInfoDialog::on_saveButton_clicked() { saveCurrentIniFile(); } - -void ModInfoDialog::on_saveTXTButton_clicked() +void ModInfoDialog::on_textFileSave_clicked() { saveCurrentTextFile(); } +void ModInfoDialog::on_textFileWordWrap_clicked() +{ + setTextFileWordWrap(!m_textFileEditor->wordWrap()); +} + +void ModInfoDialog::setTextFileWordWrap(bool b) +{ + m_textFileEditor->wordWrap(b); + ui->textFileWordWrap->setChecked(b); +} void ModInfoDialog::saveCurrentTextFile() { - QVariant fileNameVar = ui->textFileView->property("currentFile"); - QVariant encodingVar = ui->textFileView->property("encoding"); - if (fileNameVar.isValid() && encodingVar.isValid()) { - QString fileName = fileNameVar.toString(); - QFile txtFile(fileName); - txtFile.open(QIODevice::WriteOnly); - txtFile.resize(0); - QTextCodec *codec = QTextCodec::codecForName(encodingVar.toString().toUtf8()); - QString data = ui->textFileView->toPlainText().replace("\n", "\r\n"); - txtFile.write(codec->fromUnicode(data)); - } else { - reportError("no file selected"); - } - ui->saveTXTButton->setEnabled(false); + m_textFileEditor->save(); + ui->textFileSave->setEnabled(false); } @@ -1214,9 +1219,9 @@ void ModInfoDialog::on_iniFileView_textChanged() } -void ModInfoDialog::on_textFileView_textChanged() +void ModInfoDialog::onTextFileChanged(bool b) { - ui->saveTXTButton->setEnabled(true); + ui->textFileSave->setEnabled(b); } diff --git a/src/modinfodialog.h b/src/modinfodialog.h index b3ce3d07..85226f45 100644 --- a/src/modinfodialog.h +++ b/src/modinfodialog.h @@ -35,6 +35,7 @@ along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>. #include <QNetworkReply>
#include <QModelIndex>
#include <QAction>
+#include <QPlainTextEdit>
#include <QListWidgetItem>
#include <QTreeWidgetItem>
#include <QTextCodec>
@@ -49,6 +50,7 @@ namespace Ui { class QFileSystemModel;
class QTreeView;
class CategoryFactory;
+class TextEditor;
/* Takes a QToolButton and a widget and creates an expandable widget.
@@ -218,14 +220,15 @@ private slots: void on_saveButton_clicked();
void on_activateESP_clicked();
void on_deactivateESP_clicked();
- void on_saveTXTButton_clicked();
+ void on_textFileSave_clicked();
+ void on_textFileWordWrap_clicked();
void on_visitNexusLabel_linkActivated(const QString &link);
void on_modIDEdit_editingFinished();
void on_sourceGameEdit_currentIndexChanged(int);
void on_versionEdit_editingFinished();
void on_customUrlLineEdit_editingFinished();
void on_iniFileView_textChanged();
- void on_textFileView_textChanged();
+ void onTextFileChanged(bool b);
void on_tabWidget_currentChanged(int index);
void on_primaryCategoryBox_currentIndexChanged(int index);
void on_categoriesTree_itemChanged(QTreeWidgetItem *item, int column);
@@ -304,6 +307,7 @@ private: ExpanderWidget m_overwriteExpander, m_overwrittenExpander, m_nonconflictExpander;
FilterWidget m_advancedConflictFilter;
+ std::unique_ptr<TextEditor> m_textFileEditor;
void refreshConflictLists(bool refreshGeneral, bool refreshAdvanced);
@@ -347,6 +351,8 @@ private: std::vector<QAction*> createGotoActions(
const QList<QTreeWidgetItem*>& selection);
+
+ void setTextFileWordWrap(bool b);
};
#endif // MODINFODIALOG_H
diff --git a/src/modinfodialog.ui b/src/modinfodialog.ui index eed4e31f..4a29f6e4 100644 --- a/src/modinfodialog.ui +++ b/src/modinfodialog.ui @@ -27,49 +27,89 @@ </property>
<widget class="QWidget" name="tabText">
<attribute name="title">
- <string>Textfiles</string>
+ <string>Text Files</string>
</attribute>
- <layout class="QHBoxLayout" name="horizontalLayout_3">
+ <layout class="QVBoxLayout" name="verticalLayout_15">
<item>
- <widget class="QListWidget" name="textFileList">
- <property name="maximumSize">
- <size>
- <width>192</width>
- <height>16777215</height>
- </size>
- </property>
- <property name="toolTip">
- <string>A list of text-files in the mod directory.</string>
+ <widget class="QSplitter" name="tabTextSplitter">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
</property>
- <property name="whatsThis">
- <string>A list of text-files in the mod directory like readmes. </string>
+ <property name="childrenCollapsible">
+ <bool>false</bool>
</property>
+ <widget class="QListWidget" name="textFileList">
+ <property name="toolTip">
+ <string>A list of text-files in the mod directory.</string>
+ </property>
+ <property name="whatsThis">
+ <string>A list of text-files in the mod directory like readmes. </string>
+ </property>
+ </widget>
+ <widget class="QWidget" name="layoutWidget">
+ <layout class="QVBoxLayout" name="verticalLayout_9">
+ <item>
+ <widget class="QWidget" name="widget_6" native="true">
+ <layout class="QHBoxLayout" name="horizontalLayout_3">
+ <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="QToolButton" name="textFileSave">
+ <property name="text">
+ <string>Save</string>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <widget class="QToolButton" name="textFileWordWrap">
+ <property name="text">
+ <string>Word wrap</string>
+ </property>
+ <property name="checkable">
+ <bool>true</bool>
+ </property>
+ </widget>
+ </item>
+ <item>
+ <spacer name="horizontalSpacer_6">
+ <property name="orientation">
+ <enum>Qt::Horizontal</enum>
+ </property>
+ <property name="sizeHint" stdset="0">
+ <size>
+ <width>40</width>
+ <height>20</height>
+ </size>
+ </property>
+ </spacer>
+ </item>
+ </layout>
+ </widget>
+ </item>
+ <item>
+ <widget class="QPlainTextEdit" name="textFileView">
+ <property name="toolTip">
+ <string/>
+ </property>
+ <property name="whatsThis">
+ <string/>
+ </property>
+ </widget>
+ </item>
+ </layout>
+ </widget>
</widget>
</item>
- <item>
- <layout class="QVBoxLayout" name="verticalLayout_9">
- <item>
- <widget class="QTextEdit" name="textFileView">
- <property name="toolTip">
- <string/>
- </property>
- <property name="whatsThis">
- <string/>
- </property>
- </widget>
- </item>
- <item>
- <widget class="QPushButton" name="saveTXTButton">
- <property name="enabled">
- <bool>false</bool>
- </property>
- <property name="text">
- <string>Save</string>
- </property>
- </widget>
- </item>
- </layout>
- </item>
</layout>
</widget>
<widget class="QWidget" name="tabIni">
@@ -493,12 +533,12 @@ text-align: left;</string> <property name="columnCount">
<number>2</number>
</property>
- <attribute name="headerDefaultSectionSize">
- <number>365</number>
- </attribute>
<attribute name="headerMinimumSectionSize">
<number>200</number>
</attribute>
+ <attribute name="headerDefaultSectionSize">
+ <number>365</number>
+ </attribute>
<column>
<property name="text">
<string>File</string>
@@ -569,12 +609,12 @@ text-align: left;</string> <property name="columnCount">
<number>2</number>
</property>
- <attribute name="headerDefaultSectionSize">
- <number>365</number>
- </attribute>
<attribute name="headerMinimumSectionSize">
<number>200</number>
</attribute>
+ <attribute name="headerDefaultSectionSize">
+ <number>365</number>
+ </attribute>
<column>
<property name="text">
<string>File</string>
@@ -1011,7 +1051,7 @@ p, li { white-space: pre-wrap; } </layout>
</item>
<item>
- <widget class="QWebEngineView" name="descriptionView" native="true">
+ <widget class="QWebEngineView" name="descriptionView">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="MinimumExpanding">
<horstretch>0</horstretch>
@@ -1024,7 +1064,7 @@ p, li { white-space: pre-wrap; } <height>200</height>
</size>
</property>
- <property name="url" stdset="0">
+ <property name="url">
<url>
<string>about:blank</string>
</url>
diff --git a/src/texteditor.cpp b/src/texteditor.cpp new file mode 100644 index 00000000..eef74246 --- /dev/null +++ b/src/texteditor.cpp @@ -0,0 +1,76 @@ +#include "texteditor.h" +#include "utility.h" + +TextEditor::TextEditor(QPlainTextEdit* edit) + : m_edit(edit), m_dirty(false) +{ + m_edit->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); + wordWrap(true); + + QObject::connect( + m_edit->document(), &QTextDocument::modificationChanged, + [&](bool b){ onChanged(b); }); +} + +bool TextEditor::load(const QString& filename) +{ + m_filename = filename; + m_edit->setPlainText(MOBase::readFileText(filename, &m_encoding)); + m_edit->document()->setModified(false); + + return true; +} + +bool TextEditor::save() +{ + if (m_filename.isEmpty() || m_encoding.isEmpty()) { + return false; + } + + QFile file(m_filename); + file.open(QIODevice::WriteOnly); + file.resize(0); + + QTextCodec* codec = QTextCodec::codecForName(m_encoding.toUtf8()); + QString data = m_edit->toPlainText().replace("\n", "\r\n"); + + file.write(codec->fromUnicode(data)); + m_edit->document()->setModified(false); + + return true; +} + +const QString& TextEditor::filename() const +{ + return m_filename; +} + +void TextEditor::wordWrap(bool b) +{ + if (b) { + m_edit->setLineWrapMode(QPlainTextEdit::WidgetWidth); + } else { + m_edit->setLineWrapMode(QPlainTextEdit::NoWrap); + } +} + +bool TextEditor::wordWrap() const +{ + return (m_edit->lineWrapMode() == QPlainTextEdit::WidgetWidth); +} + +void TextEditor::dirty(bool b) +{ + m_dirty = b; +} + +bool TextEditor::dirty() const +{ + return m_dirty; +} + +void TextEditor::onChanged(bool b) +{ + dirty(b); + emit changed(b); +} diff --git a/src/texteditor.h b/src/texteditor.h new file mode 100644 index 00000000..25a15ad7 --- /dev/null +++ b/src/texteditor.h @@ -0,0 +1,36 @@ +#ifndef MO_TEXTEDITOR_H +#define MO_TEXTEDITOR_H + +#include <QPlainTextEdit> + +class TextEditor : public QObject +{ + Q_OBJECT + +public: + TextEditor(QPlainTextEdit* edit); + + bool load(const QString& filename); + bool save(); + + const QString& filename() const; + + void wordWrap(bool b); + bool wordWrap() const; + + bool dirty() const; + +signals: + void changed(bool b); + +private: + QPlainTextEdit* m_edit; + QString m_filename; + QString m_encoding; + bool m_dirty; + + void onChanged(bool b); + void dirty(bool b); +}; + +#endif // MO_TEXTEDITOR_H |
