From: hama Date: Sat, 29 Aug 2015 00:06:29 +0000 (+0200) Subject: ReEditor, ReMatcher X-Git-Url: https://gitweb.hamatoma.de/?a=commitdiff_plain;h=c47af363586c3b29028560a181802870e0c84207;p=reqt ReEditor, ReMatcher * ReEditor: open last file/project works * ReMatcher works --- diff --git a/appl/reditor/mainwindow.cpp b/appl/reditor/mainwindow.cpp index 37c08fb..98d5175 100644 --- a/appl/reditor/mainwindow.cpp +++ b/appl/reditor/mainwindow.cpp @@ -64,7 +64,8 @@ void MainWindow::changeProject(const QString& path) { delete m_project; m_project = new Project(path, m_logger); int maxEntries = m_workspace->intValue("history.max_projects"); - m_workspace->addHistoryEntry("projects", path, ';', maxEntries); + m_workspace->addHistoryEntry(Workspace::KEY_HISTORY_PROJECTS, path, ';', + maxEntries); } /** @@ -87,7 +88,8 @@ void MainWindow::openFile(const QString& name) { edit->setLines(m_file); edit->setCursorLine(0); int maxEntries = m_workspace->intValue("history.max_files"); - m_workspace->addHistoryEntry("files", name, ';', maxEntries); + m_workspace->addHistoryEntry(Workspace::KEY_HISTORY_FILES, name, ';', + maxEntries); } /** diff --git a/appl/reditor/projectselection.cpp b/appl/reditor/projectselection.cpp index d6a496a..7124e10 100644 --- a/appl/reditor/projectselection.cpp +++ b/appl/reditor/projectselection.cpp @@ -34,13 +34,29 @@ ProjectSelection::ProjectSelection(MainWindow* mainWindow, QWidget *parent) : SLOT(openLastFile())); connect(ui->pushButtonOpenLastProject, SIGNAL(clicked()), this, SLOT(openLastProject())); + connect(ui->lineEditFilterLastFile, SIGNAL(textChanged(QString))), this, SLOT( + textChanged(QString)); 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); + buildTable(QString(""), m_projects, ui->tableWidgetProjects); } + +/** + * Destructor. + */ +ProjectSelection::~ProjectSelection() { + delete ui; +} +void ProjectSelection::textChangedFilterFiles(const QString& text) { + +} +void ProjectSelection::textChangedFilterProjects(const QString& text) { + +} + /** * Builds the table from the table using a filter expression. * @@ -48,23 +64,48 @@ ProjectSelection::ProjectSelection(MainWindow* mainWindow, QWidget *parent) : * @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, +void ProjectSelection::buildTable(const QString& filter, QStringList& lines, QTableWidget* table) { QStringList::const_iterator it; + int rowCount = 0; for (it = lines.cbegin(); it != lines.cend(); ++it) { - bool matches = filter->isEmpty(); + bool matches = filter.isEmpty(); if (!matches) { - if (filter->startsWith("*")) { + //@ToDo: coding + if (filter.startsWith("*")) { + + } else { + + } + } + if (matches) + rowCount++; + } + table->setRowCount(rowCount); + int row = -1; + for (it = lines.cbegin(); it != lines.cend(); ++it) { + bool matches = filter.isEmpty(); + if (!matches) { + //@ToDo: coding + if (filter.startsWith("*")) { } else { } } if (matches) { + row++; QStringList cols = it->split('\t'); - + for (int col = 0; col < cols.size(); col++) { + QTableWidgetItem* item = table->item(row, col); + if (item != NULL) + item->setText(cols.at(col)); + else { + item = new QTableWidgetItem(cols.at(col)); + table->setItem(row, col, item); + } + } } - } } @@ -80,6 +121,7 @@ void ProjectSelection::buildTable(const QString* filter, QStringList& lines, */ void ProjectSelection::buildTableInfo(ReSettings* settings, const char* key, bool withDate, QStringList& tableContent) { + QStringList files; settings->historyAsList(key, files); QStringList::const_iterator it; for (it = files.cbegin(); it != files.cend(); ++it) { @@ -89,17 +131,12 @@ void ProjectSelection::buildTableInfo(ReSettings* settings, const char* key, if (withDate) info.append("\t").append( file.lastModified().toString("yyyy.mm.dd/HH:MM:SS")); - info.append(file.dir()); + info.append("\t").append(file.path()); + tableContent.append(info); } } } -/** - * Destructor. - */ -ProjectSelection::~ProjectSelection() { - delete ui; -} /** * Shows an error message. * @@ -136,15 +173,27 @@ void ProjectSelection::open() { * Opens the selected recently opened file. */ void ProjectSelection::openLastFile() { - + openSelectedItem(ui->tableWidgetFiles); } + /** * Opens the selected recently opened project (project directory). */ void ProjectSelection::openLastProject() { - + openSelectedItem(ui->tableWidgetProjects); } +QString ProjectSelection::openSelectedItem(QTableWidget* table) { + int row = max(0, table->currentRow()); + if (row < table->rowCount()) { + QString file; + int colPath = table->columnCount() - 1; + file = table->item(row, colPath)->text() + OS_SEPARATOR_STR + + table->item(row, 0)->text(); + ui->lineEditOpen->setText(file); + open(); + } +} /** * Selects a directory (project directory) with an open dialog. */ diff --git a/appl/reditor/projectselection.hpp b/appl/reditor/projectselection.hpp index 32b459e..c0ee0dd 100644 --- a/appl/reditor/projectselection.hpp +++ b/appl/reditor/projectselection.hpp @@ -11,9 +11,9 @@ #ifndef PROJECTSELECTION_HPP #define PROJECTSELECTION_HPP - +#include "reditor.hpp" #include - +#include namespace Ui { class ProjectSelection; } @@ -32,10 +32,13 @@ public slots: void selectDir(); void selectFile(); protected: - void buildTable(const QString* filter, QStringList& lines, + void buildTable(const QString& filter, QStringList& lines, QTableWidget* table); void buildTableInfo(ReSettings* settings, const char* key, bool withDate, QStringList& tableContent); + QString openSelectedItem(QTableWidget* table); + void textChangedFilterFiles(const QString& text); + void textChangedFilterProjecs(const QString& text); private: void error(const QString& message); private: diff --git a/appl/reditor/projectselection.ui b/appl/reditor/projectselection.ui index baf7cd2..5897074 100644 --- a/appl/reditor/projectselection.ui +++ b/appl/reditor/projectselection.ui @@ -6,7 +6,7 @@ 0 0 - 982 + 1034 703 @@ -77,6 +77,9 @@ Qt::Horizontal + + 3 + @@ -113,6 +116,12 @@ Use wildcards: '*' (any string) and '?' (any character) + + false + + + QAbstractItemView::NoEditTriggers + 3 @@ -124,7 +133,7 @@ Use wildcards: '*' (any string) and '?' (any character) - Name + File @@ -134,7 +143,7 @@ Use wildcards: '*' (any string) and '?' (any character) - Parent + Directory @@ -262,7 +271,7 @@ Use wildcards: '*' (any string) and '?' (any character) Opens the selected last opened project (Control-P) - Open project + Open Project Ctrl+P diff --git a/appl/reditor/workspace.cpp b/appl/reditor/workspace.cpp index f137997..809c011 100644 --- a/appl/reditor/workspace.cpp +++ b/appl/reditor/workspace.cpp @@ -11,8 +11,8 @@ #include "reditor.hpp" -const char* Workspace::KEY_HISTORY_FILES = "history.files"; -const char* Workspace::KEY_HISTORY_PROJECTS = "history.projecs"; +const char* Workspace::KEY_HISTORY_FILES = "files"; +const char* Workspace::KEY_HISTORY_PROJECTS = "projecs"; /** * Constructor. diff --git a/appl/refind/dialogoptions.ui b/appl/refind/dialogoptions.ui index 03fa9a7..5566046 100644 --- a/appl/refind/dialogoptions.ui +++ b/appl/refind/dialogoptions.ui @@ -293,7 +293,7 @@ ... - + :/main/icons/layout_add.png:/main/icons/layout_add.png @@ -385,9 +385,7 @@ - - - + buttonBox diff --git a/base/ReQStringUtils.cpp b/base/ReQStringUtils.cpp index e1f5d25..312a5b9 100644 --- a/base/ReQStringUtils.cpp +++ b/base/ReQStringUtils.cpp @@ -179,7 +179,8 @@ int ReQStringUtils::lengthOfUInt(const ReString& text, int start, int radix, * @return 0: no date found
* otherwise: the length of the date in the string */ -int ReQStringUtils::lengthOfDate(const ReString& text, int start, QDate* value) { +int ReQStringUtils::lengthOfDate(const ReString& text, int start, + QDate* value) { uint day = 0; uint month = 0; uint year = 0; @@ -290,7 +291,8 @@ int ReQStringUtils::lengthOfDateTime(const ReString& text, int start, * @return 0: no date found
* otherwise: the length of the date in the string */ -int ReQStringUtils::lengthOfTime(const ReString& text, int start, QTime* value) { +int ReQStringUtils::lengthOfTime(const ReString& text, int start, + QTime* value) { uint hour = 0; uint minute = 0; uint sec = 0; @@ -809,3 +811,72 @@ QDateTime ReDateTimeParser::parseDateTime(const QString& expr) { return rc; } +/** + * Constructor. + * + * @param pattern a pattern with wildcards '*' (any string) + * @param anchored true: the pattern starts at the strings start
+ * false: the pattern can start anywhere in the string
+ */ + +ReMatcher::ReMatcher(const QString& pattern, bool anchored) : + m_needles(pattern.split(QChar('*'))), + m_restLengths(), + m_anchored(anchored) { + for (int ix = 0; ix < m_needles.size(); ix++) { + m_restLengths.append(0); + } + int sum = 0; + for (int ix = m_needles.size() - 1; ix >= 0; ix--) { + int size = m_needles.at(ix).size(); + m_restLengths.append(size + sum); + sum += size; + } +} + +/** + * Destructor. + */ +ReMatcher::~ReMatcher() { +} + +/** + * Tests whether the given string matches the pattern. + * + * @param text the text to test + * @return true: the pattern matches + */ +bool ReMatcher::matches(const QString& text) { + bool found = false; + if (m_anchored) + found = matches(text, 0, 0); + else { + found = text.startsWith(m_needles.at(0)); + if (found && m_needles.size() > 1) + found = matches(text, m_needles.at(0).length(), 1); + } + return found; +} + +/** + * Recursive matching test. + * + * @param text text to inspect + * @param textIndex the first index in text to inspect + * @param needleIndex the index of the pattern part to test + * @return true: the needles from the given index + * up to the last index matches + */ +bool ReMatcher::matches(const QString& text, int textIndex, int needleIndex) { + int ix = textIndex; + QString needle = m_needles.at(needleIndex); + bool found = text.size() - textIndex >= m_restLengths.at(needleIndex); + while (!found && (ix = text.indexOf(needle, textIndex)) >= 0) { + textIndex = ix; + if (needleIndex >= m_needles.size() - 1) + found = true; + else + found = matches(text, ix + needle.length(), needleIndex); + } + return found; +} diff --git a/base/ReQStringUtils.hpp b/base/ReQStringUtils.hpp index 0552f9d..e929150 100644 --- a/base/ReQStringUtils.hpp +++ b/base/ReQStringUtils.hpp @@ -31,6 +31,7 @@ public: 10, uint64_t* value = NULL); static int lengthOfUInt(const ReString& text, int start, int radix, uint* pValue); + static bool match(const QString& heap, const QStringList& needles); /** * Returns the path with native path separators. * @@ -102,4 +103,19 @@ private: QDateTime m_dateTime; }; +class ReMatcher { +public: + ReMatcher(const QString& pattern, bool anchored = false); + virtual ~ReMatcher(); +public: + virtual bool matches(const QString& text); +protected: + bool matches(const QString& text, int textIndex, int needleIndex); +protected: + QStringList m_needles; + // m_restLengths[ix] = sum(m_needles[ix..last].size() + QList m_restLengths; + bool m_anchored; +}; + #endif // RPLQSTRING_HPP diff --git a/cunit/allTests.cpp b/cunit/allTests.cpp index 2010274..749e4ff 100644 --- a/cunit/allTests.cpp +++ b/cunit/allTests.cpp @@ -39,10 +39,10 @@ static void testBase() { void testReWriter(); void testReFile(); void testReFileUtils(); + testReQStringUtil(); testReFile(); testReFileUtils(); if (s_allTest) { - testReQStringUtil(); testReByteStorage(); testReCharPtrMap(); testReConfig(); @@ -85,8 +85,8 @@ static void testOs() { } void allTests() { - testGui(); testBase(); + testGui(); if (s_allTest) { testBase(); testMath(); diff --git a/cunit/cuReQStringUtils.cpp b/cunit/cuReQStringUtils.cpp index 3c06e38..513cb52 100644 --- a/cunit/cuReQStringUtils.cpp +++ b/cunit/cuReQStringUtils.cpp @@ -17,7 +17,7 @@ class TestReQStringUtil: public ReTest { public: TestReQStringUtil() : - ReTest("ReQStringUtil") { + ReTest("ReQStringUtil") { doIt(); } @@ -35,19 +35,20 @@ public: void testLengthOfUInt64() { quint64 value = -3; checkEqu(1, - ReQStringUtils::lengthOfUInt64(ReString("0"), 0, 10, &value)); + ReQStringUtils::lengthOfUInt64(ReString("0"), 0, 10, &value)); checkEqu(int64_t(0), value); checkEqu(3, ReQStringUtils::lengthOfUInt64("x432", 1, 10, &value)); checkEqu(int64_t(432LL), value); checkEqu(3, ReQStringUtils::lengthOfUInt64("x432 x", 1, 10, &value)); checkEqu(int64_t(432LL), value); - checkEqu(3, ReQStringUtils::lengthOfUInt64("x432fabc x", 1, 10, &value)); + checkEqu(3, + ReQStringUtils::lengthOfUInt64("x432fabc x", 1, 10, &value)); checkEqu(int64_t(432LL), value); checkEqu(16, - ReQStringUtils::lengthOfUInt64("a1234567890123567", 1, 10, &value)); + ReQStringUtils::lengthOfUInt64("a1234567890123567", 1, 10, &value)); checkEqu(int64_t(1234567890123567LL), value); checkEqu(10, - ReQStringUtils::lengthOfUInt64("x1234abcdef", 1, 16, &value)); + ReQStringUtils::lengthOfUInt64("x1234abcdef", 1, 16, &value)); checkEqu(int64_t(0x1234abcdefLL), value); checkEqu(3, ReQStringUtils::lengthOfUInt64("432", 0, 8, &value)); checkEqu(int64_t(0432LL), value); @@ -84,24 +85,27 @@ public: checkEqu(1, ReQStringUtils::lengthOfReal(ReString(" 0"), 1, &value)); checkEqu(0.0, value); checkEqu(17, - ReQStringUtils::lengthOfReal(ReString("X12345678901234567"), 1, - &value)); + ReQStringUtils::lengthOfReal(ReString("X12345678901234567"), 1, + &value)); checkEqu(12345678901234567.0, value); checkEqu(2, ReQStringUtils::lengthOfReal(ReString(".5"), 0, &value)); checkEqu(0.5, value); - checkEqu(5, ReQStringUtils::lengthOfReal(ReString("2.5e2x"), 0, &value)); + checkEqu(5, + ReQStringUtils::lengthOfReal(ReString("2.5e2x"), 0, &value)); checkEqu(250.0, value); - checkEqu(6, ReQStringUtils::lengthOfReal(ReString("2.5e+2"), 0, &value)); + checkEqu(6, + ReQStringUtils::lengthOfReal(ReString("2.5e+2"), 0, &value)); checkEqu(250.0, value); checkEqu(7, - ReQStringUtils::lengthOfReal(ReString("2.5E-33"), 0, &value)); + ReQStringUtils::lengthOfReal(ReString("2.5E-33"), 0, &value)); checkEqu(2.5e-33, value); checkEqu(3, ReQStringUtils::lengthOfReal(ReString("2.5E"), 0, &value)); checkEqu(2.5, value); checkEqu(3, ReQStringUtils::lengthOfReal(ReString("2.5E+"), 0, &value)); checkEqu(2.5, value); - checkEqu(3, ReQStringUtils::lengthOfReal(ReString("2.5E-a"), 0, &value)); + checkEqu(3, + ReQStringUtils::lengthOfReal(ReString("2.5E-a"), 0, &value)); checkEqu(2.5, value); } @@ -124,7 +128,7 @@ public: ReString name = "Heinz Müller"; char buffer[32]; checkEqu("Heinz Müller", - ReQStringUtils::utf8(name, buffer, sizeof buffer)); + ReQStringUtils::utf8(name, buffer, sizeof buffer)); memset(buffer, 'x', sizeof buffer); checkEqu("Heinz", ReQStringUtils::utf8(name, buffer, (size_t)(5 + 1))); checkEqu(buffer[6], 'x'); @@ -182,8 +186,20 @@ public: checkEqu(8, ReQStringUtils::lengthOfTime("301:02:09x", 1, &time)); checkEqu(QTime(1, 2, 9), time); } + void testReMatcher() { + ReMatcher m1("a*b*c", true); + checkT(m1.matches("a b c d")); + checkT(m1.matches("abc d")); + checkF(m1.matches("ababc")); + ReMatcher m2("a*b*c", false); + + checkT(m2.matches("a b c d")); + checkT(m2.matches("ababc")); + checkT(m2.matches("a b a b c")); + } virtual void run(void) { + testReMatcher(); testLengtOfTime(); testLengtOfDate(); testDateTimeParser();