From: hama Date: Tue, 25 Aug 2015 22:20:59 +0000 (+0200) Subject: cuReEdit, cuReSettings without errors X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=087d1098493cd43e9789b6d2a634be1c5af8648d;p=reqt cuReEdit, cuReSettings without errors --- diff --git a/appl/reditor/mainwindow.cpp b/appl/reditor/mainwindow.cpp index 341dd7f..37c08fb 100644 --- a/appl/reditor/mainwindow.cpp +++ b/appl/reditor/mainwindow.cpp @@ -114,11 +114,11 @@ ReLogger* MainWindow::logger() const { * * @return the current workspace */ -ReSettings* MainWindow::project() const { +Project* MainWindow::project() const { return m_project; } -ReSettings* MainWindow::workspace() const { +Workspace* MainWindow::workspace() const { return m_workspace; } diff --git a/appl/reditor/mainwindow.hpp b/appl/reditor/mainwindow.hpp index bb5757c..b9485f6 100644 --- a/appl/reditor/mainwindow.hpp +++ b/appl/reditor/mainwindow.hpp @@ -31,8 +31,8 @@ public: void changeWorkspace(const QString& path); void closeProject(); void openFile(const QString& name); - Storage* project() const; - Storage* workspace() const; + Project* project() const; + Workspace* workspace() const; ReLogger* logger() const; public slots: @@ -40,8 +40,8 @@ public slots: private: Ui::MainWindow *ui; ReFile* m_file; - Storage* m_project; - Storage* m_workspace; + Project* m_project; + Workspace* m_workspace; ReLogger* m_logger; }; diff --git a/appl/reditor/project.hpp b/appl/reditor/project.hpp index e088f47..6bca375 100644 --- a/appl/reditor/project.hpp +++ b/appl/reditor/project.hpp @@ -12,7 +12,7 @@ #ifndef PROJECT_HPP #define PROJECT_HPP -class Project: public Storage { +class Project: public ReSettings { public: Project(const QString& path, ReLogger* logger); }; diff --git a/appl/reditor/projectselection.cpp b/appl/reditor/projectselection.cpp index 8550f56..d6a496a 100644 --- a/appl/reditor/projectselection.cpp +++ b/appl/reditor/projectselection.cpp @@ -34,6 +34,64 @@ ProjectSelection::ProjectSelection(MainWindow* mainWindow, QWidget *parent) : SLOT(openLastFile())); connect(ui->pushButtonOpenLastProject, SIGNAL(clicked()), this, SLOT(openLastProject())); + Workspace* workspace = mainWindow->workspace(); + buildTableInfo(workspace, Workspace::KEY_HISTORY_FILES, true, m_files); + buildTableInfo(workspace, Workspace::KEY_HISTORY_PROJECTS, false, + m_projects); + buildTable("", m_files, ui->tableWidgetFiles); + buildTable("", m_projects, ui->tableWidgetProjects); +} +/** + * Builds the table from the table using a filter expression. + * + * @param filter a filter expression with wildcards '*' + * @param lines the full table info + * @param table OUT: will be filled with all lines matching the filter + */ +void ProjectSelection::buildTable(const QString* filter, QStringList& lines, + QTableWidget* table) { + QStringList::const_iterator it; + for (it = lines.cbegin(); it != lines.cend(); ++it) { + bool matches = filter->isEmpty(); + if (!matches) { + if (filter->startsWith("*")) { + + } else { + + } + } + if (matches) { + QStringList cols = it->split('\t'); + + } + + } +} + +/** + * Build the info for a table (last opened files or last opened projects). + * + * Note: the table shows a filtered part of this info. + * + * @param settings the history container + * @param key the name of the history entry + * @param withDate the file's name is part of the info + * @param tableContent OUT: the list containing the table info + */ +void ProjectSelection::buildTableInfo(ReSettings* settings, const char* key, + bool withDate, QStringList& tableContent) { + settings->historyAsList(key, files); + QStringList::const_iterator it; + for (it = files.cbegin(); it != files.cend(); ++it) { + QFileInfo file(*it); + if (file.exists()) { + QString info = file.baseName(); + if (withDate) + info.append("\t").append( + file.lastModified().toString("yyyy.mm.dd/HH:MM:SS")); + info.append(file.dir()); + } + } } /** diff --git a/appl/reditor/projectselection.hpp b/appl/reditor/projectselection.hpp index 0c4913b..32b459e 100644 --- a/appl/reditor/projectselection.hpp +++ b/appl/reditor/projectselection.hpp @@ -31,11 +31,18 @@ public slots: void openLastProject(); void selectDir(); void selectFile(); +protected: + void buildTable(const QString* filter, QStringList& lines, + QTableWidget* table); + void buildTableInfo(ReSettings* settings, const char* key, bool withDate, + QStringList& tableContent); private: void error(const QString& message); private: Ui::ProjectSelection *ui; MainWindow* m_mainWindow; + QStringList m_files; + QStringList m_projects; }; #endif // PROJECTSELECTION_HPP diff --git a/appl/reditor/reditor.pro b/appl/reditor/reditor.pro index ede1294..2d6441f 100644 --- a/appl/reditor/reditor.pro +++ b/appl/reditor/reditor.pro @@ -16,7 +16,7 @@ INCLUDEPATH += ../.. SOURCES += \ ../../gui/ReEdit.cpp \ ../../gui/ReStateStorage.cpp \ - ../../gui/ReStorage.cpp \ + ../../gui/ReSettings.cpp \ ../../base/ReFile.cpp \ mainwindow.cpp \ ../../base/ReLogger.cpp \ diff --git a/appl/reditor/workspace.cpp b/appl/reditor/workspace.cpp index 5b5c64e..f137997 100644 --- a/appl/reditor/workspace.cpp +++ b/appl/reditor/workspace.cpp @@ -11,6 +11,9 @@ #include "reditor.hpp" +const char* Workspace::KEY_HISTORY_FILES = "history.files"; +const char* Workspace::KEY_HISTORY_PROJECTS = "history.projecs"; + /** * Constructor. * diff --git a/appl/reditor/workspace.hpp b/appl/reditor/workspace.hpp index 4be110b..8776fed 100644 --- a/appl/reditor/workspace.hpp +++ b/appl/reditor/workspace.hpp @@ -12,7 +12,10 @@ #ifndef WORKSPACE_HPP #define WORKSPACE_HPP -class Workspace: public Storage { +class Workspace: public ReSettings { +public: + static const char* KEY_HISTORY_FILES; + static const char* KEY_HISTORY_PROJECTS; public: Workspace(const QString& path, ReLogger* logger); }; diff --git a/cunit/cuReEdit.cpp b/cunit/cuReEdit.cpp index 9fabc5e..ea7f7eb 100644 --- a/cunit/cuReEdit.cpp +++ b/cunit/cuReEdit.cpp @@ -18,8 +18,8 @@ class TestReEdit: public ReTest, protected ReEdit { public: TestReEdit() : - ReTest("ReEdit"), - ReEdit(NULL) { + ReTest("ReEdit"), + ReEdit(NULL) { doIt(); } @@ -31,7 +31,7 @@ public: setLines(&m_lines); } void pushKey(int key, const QString& text = "", - Qt::KeyboardModifiers modifiers = Qt::NoModifier) { + Qt::KeyboardModifiers modifiers = Qt::NoModifier) { QKeyEvent event(QEvent::KeyPress, key, modifiers, text); keyPressEvent(&event); } @@ -69,7 +69,7 @@ public: checkEqu(expectedCol, m_cursorCol); } void checkCursorPos(EditorAction action, int expectedLineNo, - int expectedCol) { + int expectedCol) { editorAction(action); checkEqu(expectedLineNo, m_cursorLineNo); checkEqu(expectedCol, m_cursorCol); @@ -294,7 +294,7 @@ public: checkEqu("abcd", m_lines.lineAt(0)); checkEqu("1234", m_lines.lineAt(1)); checkEqu(-1, m_cursorCol); - checkEqu(2, m_cursorLineNo); + checkEqu(1, m_cursorLineNo); // wrong line numbers: init("abcd\n1234\nxy"); @@ -313,7 +313,7 @@ public: checkEqu("xy", m_lines.lineAt(2)); log("ok"); -} + } virtual void run() { testDeleteLine(); testDeleteText(); diff --git a/cunit/cuReSettings.cpp b/cunit/cuReSettings.cpp index f79c3f4..6f96ead 100644 --- a/cunit/cuReSettings.cpp +++ b/cunit/cuReSettings.cpp @@ -20,12 +20,12 @@ class TestReSettings: public ReTest { public: TestReSettings() : - ReTest("ReSettings") { + ReTest("ReSettings") { doIt(); } public: - void testAddHistoryEntry(){ + void testAddHistoryEntry() { QByteArray dir(ReFile::tempDir("resettings", NULL, false)); ReFile::deleteTree((QString) dir, false, &m_logger); { @@ -40,17 +40,19 @@ public: settings.historyAsList("digits", list); checkEqu("1 2 3", list.join(' ')); } - void setSettings(ReSettings& settings){ - settings.insertProperty(new ReProperty("level1.boolVal", "Boolean", - "Boolean value for test", ReSettings::TRUE, PT_BOOL)); - settings.insertProperty(new ReProperty("level1.intVal", "Integer", - "Integer value for test", "4711", PT_INT, "[0,9999]")); - settings.insertProperty(new ReProperty("level2.strVal", "String", - "String value for test", "crazy horse", PT_STRING)); - + void setSettings(ReSettings& settings) { + settings.insertProperty( + new ReProperty("level1.boolVal", "Boolean", + "Boolean value for test", ReSettings::TRUE, PT_BOOL)); + settings.insertProperty( + new ReProperty("level1.intVal", "Integer", "Integer value for test", + "4711", PT_INT, "[0,9999]")); + settings.insertProperty( + new ReProperty("level2.strVal", "String", "String value for test", + "crazy horse", PT_STRING)); } - void testBasic(){ + void testBasic() { QByteArray dir(ReFile::tempDir("resettings", NULL, false)); ReFile::deleteTree((QString) dir, false, &m_logger); ReSettings settings(dir, "test", &m_memoryLogger); @@ -74,7 +76,7 @@ public: setSettings(settings2); settings2.changeValue("level1.boolVal", ReSettings::FALSE); settings2.changeValue("level1.intVal", "1234"); - settings2.changeValue("level2.strVal", "pretty women"); + settings2.changeValue("level2.strVal", "pretty woman"); settings2.writeSettings(); settings.readSettings(); diff --git a/gui/ReSettings.cpp b/gui/ReSettings.cpp index 2133768..fcea3be 100644 --- a/gui/ReSettings.cpp +++ b/gui/ReSettings.cpp @@ -40,14 +40,16 @@ QString ReSettings::FALSE = ""; * @param type the type of the property * @param limits NULL or some rules for the property value */ -ReProperty::ReProperty(const char* name, const QString& title, const QString& description, const QString& defaultValue, RePropertyType type, const char* limits) : - m_name(name), - m_title(title), - m_description(description), - m_value(defaultValue), - m_defaultValue(defaultValue), - m_type(type), - m_limits(limits) { +ReProperty::ReProperty(const char* name, const QString& title, + const QString& description, const QString& defaultValue, + RePropertyType type, const char* limits) : + m_name(name), + m_title(title), + m_description(description), + m_value(defaultValue), + m_defaultValue(defaultValue), + m_type(type), + m_limits(limits) { } /** @@ -56,30 +58,27 @@ ReProperty::ReProperty(const char* name, const QString& title, const QString& de * @param error OUT: NULL or the error message * @return true: the value is allowed
*/ -bool ReProperty::isValid(const QString& value, QString* error) -{ +bool ReProperty::isValid(const QString& value, QString* error) { bool rc = true; if (error != NULL) *error = ""; - if (m_limits != NULL){ - switch(m_type){ + if (m_limits != NULL) { + switch (m_type) { case PT_BOOL: break; - case PT_INT: - { + case PT_INT: { int nValue = value.toInt(&rc); - if (! rc){ + if (!rc) { if (error != NULL) *error = QObject::tr("not an integer: ") + value; } else { int minimum, maximum; - if (sscanf(m_limits, "[%d,%d]", &minimum, &maximum) == 2) - { - if (nValue < minimum){ + if (sscanf(m_limits, "[%d,%d]", &minimum, &maximum) == 2) { + if (nValue < minimum) { rc = false; if (error != NULL) *error = value + " < " + minimum; - } else if (nValue > maximum){ + } else if (nValue > maximum) { rc = false; if (error != NULL) *error = value + " < " + maximum; @@ -103,14 +102,14 @@ bool ReProperty::isValid(const QString& value, QString* error) * @param path the parent directory for the storage files * @param prefix type of the storage: "proj" or "ws" (workspace) */ -ReSettings::ReSettings(const QString& path, const QString prefix, ReLogger* logger) : - m_path(path), - m_fileHistory(path + OS_SEPARATOR + prefix + ".history"), - m_fileSettings( - path + OS_SEPARATOR + prefix + ".settings"), - m_settings(), - m_chapters(), - m_logger(logger) { +ReSettings::ReSettings(const QString& path, const QString prefix, + ReLogger* logger) : + m_path(path), + m_fileHistory(path + OS_SEPARATOR + prefix + ".history"), + m_fileSettings(path + OS_SEPARATOR + prefix + ".settings"), + m_settings(), + m_chapters(), + m_logger(logger) { } @@ -127,8 +126,9 @@ ReSettings::ReSettings(const QString& path, const QString prefix, ReLogger* logg * @param form the prefix of the key. If NULL the current form will be taken */ void ReSettings::addHistoryEntry(const char* key, const QString& value, - char separator, int maxEntries) { + char separator, int maxEntries) { ReStateStorage store(m_fileHistory); + store.initForRead(); store.addHistoryEntry(key, value, separator, maxEntries); store.close(); store.flushMap(); @@ -145,10 +145,10 @@ bool ReSettings::boolValue(const char* name) { ReProperty* property = m_settings.value(name, NULL); if (property == NULL) m_logger->logv(LOG_ERROR, LOC_BOOL_VALUE_1, "missing bool property %s", - name); + name); else if (property->m_type != PT_BOOL) m_logger->logv(LOG_ERROR, LOC_BOOL_VALUE_2, "not a bool property %s", - name); + name); else rc = !property->m_value.isEmpty(); return rc; @@ -160,17 +160,16 @@ bool ReSettings::boolValue(const char* name) { * @param name the name of the property * @param value the new value */ -void ReSettings::changeValue(const char* name, const QString& value) -{ +void ReSettings::changeValue(const char* name, const QString& value) { QString error; ReProperty* property = m_settings.value(name, NULL); if (property == NULL) m_logger->logv(LOG_ERROR, LOC_CHANGE_VALUE_1, "unknown property: %s", - name); + name); else if (!property->isValid(value, &error)) - m_logger->logv(LOG_ERROR, LOC_CHANGE_VALUE_2, "invalid value for %s: %s\n+++ %s", - name, value.toUtf8().constData(), - error.toUtf8().constData()); + m_logger->logv(LOG_ERROR, LOC_CHANGE_VALUE_2, + "invalid value for %s: %s\n+++ %s", name, + value.toUtf8().constData(), error.toUtf8().constData()); else property->m_value = value; } @@ -184,8 +183,9 @@ void ReSettings::changeValue(const char* name, const QString& value) * @return list (for chaining) */ QStringList&ReSettings::historyAsList(const char* key, QStringList& list, - const char* form) { + const char* form) { ReStateStorage store(m_fileHistory); + store.initForRead(); QStringList& rc = store.historyAsList(key, list, form); store.close(); return rc; @@ -219,10 +219,10 @@ int ReSettings::intValue(const char* name) { ReProperty* property = m_settings.value(name, NULL); if (property == NULL) m_logger->logv(LOG_ERROR, LOC_INT_VALUE_1, "missing int property %s", - name); + name); else if (property->m_type != PT_INT) m_logger->logv(LOG_ERROR, LOC_INT_VALUE_2, "not a int property %s", - name); + name); else rc = property->m_value.toInt(); return rc; @@ -240,13 +240,12 @@ QString ReSettings::path() const { /** * Reads the configuration file. */ -void ReSettings::readSettings() -{ +void ReSettings::readSettings() { QFile file(m_fileSettings); if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) - m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_1, "cannot open (%d): ", - errno, m_fileSettings.toUtf8().constData()); - else{ + m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_1, "cannot open (%d): ", + errno, m_fileSettings.toUtf8().constData()); + else { QTextStream input(&file); int lineNo = 0; while (!input.atEnd()) { @@ -254,31 +253,34 @@ void ReSettings::readSettings() QString line = input.readLine(); int ix = line.indexOf('='); if (ix < 0) - m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_2, "missing '=': %s-%d: %s", - m_fileSettings.toUtf8().constData(), lineNo, - line.mid(0, 20).toUtf8().constData()); - else if (ix == 0 || ix == 1 && line.at(0) == '!') - m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_3, "line starts with '=': %s-%d: %s", - m_fileSettings.toUtf8().constData(), lineNo, - line.mid(0, 20).toUtf8().constData()); + m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_2, + "missing '=': %s-%d: %s", + m_fileSettings.toUtf8().constData(), lineNo, + line.mid(0, 20).toUtf8().constData()); + else if (ix == 0 || (ix == 1 && line.at(0) == '!')) + m_logger->logv(LOG_ERROR, LOC_READ_SETTINGS_3, + "line starts with '=': %s-%d: %s", + m_fileSettings.toUtf8().constData(), lineNo, + line.mid(0, 20).toUtf8().constData()); else { QByteArray name; QString value; - if (line.at(ix-1) == '!'){ + if (line.at(ix - 1) == '!') { name = line.left(ix - 1).toUtf8(); value = line.mid(ix + 1); - value.replace("\\\\", "\01").replace("\\n", "\n") - .replace("\\r", "\r").replace('\01', '\\'); + value.replace("\\\\", "\01").replace("\\n", "\n").replace( + "\\r", "\r").replace('\01', '\\'); } else { name = line.left(ix).toUtf8(); value = line.mid(ix + 1); } ReProperty* property = m_settings.value(name, NULL); - if (property != NULL){ - switch(property->m_type){ + if (property != NULL) { + switch (property->m_type) { case PT_BOOL: - property->m_value = value.isEmpty() ? ReSettings::FALSE - : ReSettings::TRUE; + property->m_value = + value.isEmpty() ? + ReSettings::FALSE : ReSettings::TRUE; break; case PT_INT: if (property->isValid(value)) @@ -309,10 +311,10 @@ QString ReSettings::stringValue(const char* name) { ReProperty* property = m_settings.value(name, NULL); if (property == NULL) m_logger->logv(LOG_ERROR, LOC_STRING_VALUE_1, - "missing string property %s", name); + "missing string property %s", name); else if (property->m_type != PT_STRING) m_logger->logv(LOG_ERROR, LOC_STRING_VALUE_2, - "not a string property %s", name); + "not a string property %s", name); else rc = property->m_value; return rc; @@ -321,29 +323,28 @@ QString ReSettings::stringValue(const char* name) { /** * Writes the values into the configuration file. */ -void ReSettings::writeSettings() -{ +void ReSettings::writeSettings() { QFile file(m_fileSettings); if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) - m_logger->logv(LOG_ERROR, LOC_WRITE_SETTINGS_1, "cannot open (%d): ", - errno, m_fileSettings.toUtf8().constData()); - else{ + m_logger->logv(LOG_ERROR, LOC_WRITE_SETTINGS_1, "cannot open (%d): ", + errno, m_fileSettings.toUtf8().constData()); + else { QTextStream out(&file); - QMap::const_iterator it; - for (it = m_settings.cbegin(); it != m_settings.cend(); ++it){ + QMap::const_iterator it; + for (it = m_settings.cbegin(); it != m_settings.cend(); ++it) { ReProperty* property = it.value(); - if (property->m_value == property->m_defaultValue){ + if (property->m_value == property->m_defaultValue) { // we do not write the default value } else if (it.value()->m_value.indexOf('\n') <= 0) out << it.key() << "=" << it.value()->m_value << "\n"; - else{ + else { QString value = it.value()->m_value; - out << it.key() << "!=" << value.replace("\\", "\\\\") - .replace("\n", "\\n").replace("\r", "\\r") << "\n"; + out << it.key() << "!=" + << value.replace("\\", "\\\\").replace("\n", "\\n").replace( + "\r", "\\r") << "\n"; } } file.close(); } } - diff --git a/gui/ReSettings.hpp b/gui/ReSettings.hpp index 210c7bd..3523d1f 100644 --- a/gui/ReSettings.hpp +++ b/gui/ReSettings.hpp @@ -21,9 +21,9 @@ enum RePropertyType { class ReProperty { public: - ReProperty(const char* name, const QString& title, const QString& description, - const QString& defaultValue, RePropertyType type, const char* limits = - NULL); + ReProperty(const char* name, const QString& title, + const QString& description, const QString& defaultValue, + RePropertyType type, const char* limits = NULL); public: bool isValid(const QString& value, QString* error = NULL); public: @@ -44,11 +44,11 @@ public: ReSettings(const QString& path, const QString prefix, ReLogger* logger); public: void addHistoryEntry(const char* key, const QString& value, char separator, - int maxEntries); + int maxEntries); bool boolValue(const char* name); void changeValue(const char* name, const QString& value); QStringList& historyAsList(const char* key, QStringList& list, - const char* form = NULL); + const char* form = NULL); void insertProperty(ReProperty* property); int intValue(const char* name); QString path() const; @@ -59,7 +59,7 @@ protected: QString m_path; QString m_fileHistory; QString m_fileSettings; - QMap m_settings; + QMap m_settings; QMap*> m_chapters; ReLogger* m_logger; };