From 88b634ad3e58838bb255546349c2483f819f2ce7 Mon Sep 17 00:00:00 2001 From: alesapin Date: Sat, 17 Sep 2016 17:37:28 +0300 Subject: [PATCH] Added buttons and delete logic changed --- Matches/matchesbasicwidget.cpp | 2 - Patterns/patternlistmodel.cpp | 66 +++++++++++++++++++++++++------- Patterns/patternlistmodel.h | 4 ++ Patterns/patternsbasicwidget.cpp | 53 +++++++++++++++++++++++-- Patterns/patternsbasicwidget.h | 11 +++++- Patterns/patternslist.cpp | 27 +++++++------ Patterns/patternslist.h | 5 ++- Text/matchtextviewer.cpp | 1 - Text/textbasicwidget.cpp | 10 +---- Text/textbasicwidget.h | 2 - centralwidget.cpp | 8 +++- 11 files changed, 141 insertions(+), 48 deletions(-) diff --git a/Matches/matchesbasicwidget.cpp b/Matches/matchesbasicwidget.cpp index ad34552..bcd5b2c 100644 --- a/Matches/matchesbasicwidget.cpp +++ b/Matches/matchesbasicwidget.cpp @@ -92,8 +92,6 @@ void MatchesBasicWidget::slotDeselectAllClicked() { list->slotDeselectAll(); list->slotSetUserCheckable(false); - selectAll->setEnabled(false); - deselectAll->setEnabled(false); table->slotHideAll(); emit hideAll(); } diff --git a/Patterns/patternlistmodel.cpp b/Patterns/patternlistmodel.cpp index 02982fc..55b5e8d 100644 --- a/Patterns/patternlistmodel.cpp +++ b/Patterns/patternlistmodel.cpp @@ -163,7 +163,17 @@ bool PatternListModel::removeRows(int row, int count, const QModelIndex &parent) { Q_UNUSED(parent); beginRemoveRows(QModelIndex(), row, row + count - 1); - while(count--) rowData.removeAt(row); + + while(count--) { + int index = positions[rowData[row].name]; + positions.remove(rowData[row].name); + for (auto itr = positions.begin(); itr != positions.end(); ++itr){ + if (itr.value() > index) { + positions[itr.key()]--; + } + } + rowData.removeAt(row); + } endRemoveRows(); return true; } @@ -211,6 +221,48 @@ void PatternListModel::swapRows(int first, int second) emit dataChanged(createIndex(first,0),createIndex(second,0)); } +QVector PatternListModel::getSelectedPatterns() const +{ + QVector result; + for (int i =0; i < rowData.size(); ++i){ + if(rowData[i].checked) result.push_front(i); + } + return result; +} + +QStringList PatternListModel::getSelectedNames() const +{ + QStringList result; + for(int i =0;i>(QDataStream& istream, PatternListModel::ListItem& ms) -//{ -// istream >> ms.name >> ms.text; -// return istream; -//} diff --git a/Patterns/patternlistmodel.h b/Patterns/patternlistmodel.h index 1240ad4..33785c2 100644 --- a/Patterns/patternlistmodel.h +++ b/Patterns/patternlistmodel.h @@ -51,6 +51,10 @@ class PatternListModel : public QAbstractTableModel Qt::ItemFlags flags(const QModelIndex &index) const Q_DECL_OVERRIDE; QMimeData *mimeData(const QModelIndexList &indexes) const Q_DECL_OVERRIDE; void swapRows(int first,int second); + QVector getSelectedPatterns() const; + QStringList getSelectedNames() const; + void checkAll() ; + void resetAll(); private: QWidget* par; diff --git a/Patterns/patternsbasicwidget.cpp b/Patterns/patternsbasicwidget.cpp index c51b969..71d3fa6 100644 --- a/Patterns/patternsbasicwidget.cpp +++ b/Patterns/patternsbasicwidget.cpp @@ -8,19 +8,28 @@ void PatternsBasicWidget::initPatternEditor() buttonBar->setMovable(false); buttonBar->setFloatable(false); - compileButton = new QPushButton(tr("Скомпилировать"),this); - clearAllButton= new QPushButton(tr("Удалить всё"),this); + compileButton = new QPushButton(tr("Компиляция"),this); + matchButton = new QPushButton(tr("Сопоставление"), this); + clearButton= new QPushButton(tr("Удалить"),this); addButton = new QPushButton(tr("Добавить"),this); + selectAllButton = new QPushButton(tr("Выбрать всё"),this); + deselectAllButton = new QPushButton(tr("Сбросить всё"),this); buttonBar->addWidget(compileButton); - buttonBar->addWidget(clearAllButton); + buttonBar->addWidget(matchButton); + buttonBar->addWidget(selectAllButton); + buttonBar->addWidget(deselectAllButton); + buttonBar->addWidget(clearButton); QWidget* container =new QWidget(this); QVBoxLayout* lay = new QVBoxLayout(container); lay->setContentsMargins(0,0,0,0); list = new PatternsList(comp,this); editor = new PatternEditor(this); connect(compileButton,SIGNAL(clicked(bool)),list,SLOT(slotCompilePatterns())); - connect(clearAllButton,SIGNAL(clicked(bool)),this,SLOT(slotClearPatterns())); + connect(clearButton,SIGNAL(clicked(bool)),this,SLOT(slotDeleteSelected())); + connect(matchButton,SIGNAL(clicked(bool)),this,SLOT(slotMatchClicked())); connect(addButton,SIGNAL(clicked(bool)),this,SLOT(slotAddPattern())); + connect(selectAllButton,SIGNAL(clicked(bool)),list,SLOT(slotSelectAll())); + connect(deselectAllButton,SIGNAL(clicked(bool)),list,SLOT(slotDeselectAll())); connect(list,SIGNAL(editPatternSignal(QString)),editor,SLOT(setText(QString))); QHBoxLayout* line = new QHBoxLayout(); line->addWidget(editor); @@ -120,6 +129,42 @@ void PatternsBasicWidget::slotAddPattern() editor->clean(); } +void PatternsBasicWidget::slotEnableMatch() +{ + matchButton->setEnabled(true); +} + +void PatternsBasicWidget::slotMatchClicked() +{ + matchButton->setDisabled(true); + emit matchClicked(); +} + +void PatternsBasicWidget::slotDisableMatch() +{ + matchButton->setDisabled(true); +} + +void PatternsBasicWidget::slotDeleteSelected() +{ + QStringList selected = list->getSelectedPatternNames(); + if(!selected.empty()) { + QMessageBox msgBox( + QMessageBox::Information, + tr("Удалить выбранные шаблоны?"), + "Данные шаблоны будут удалены: "+ selected.join(',')+ ".", + QMessageBox::Yes | QMessageBox::No + ); + msgBox.setButtonText(QMessageBox::Yes, tr("Да")); + msgBox.setButtonText(QMessageBox::No, tr("Нет")); + + if (msgBox.exec() == QMessageBox::Yes) { + list->slotDeleteChecked(); + } + } +} + + diff --git a/Patterns/patternsbasicwidget.h b/Patterns/patternsbasicwidget.h index 0bc0527..ab56c2b 100644 --- a/Patterns/patternsbasicwidget.h +++ b/Patterns/patternsbasicwidget.h @@ -33,7 +33,10 @@ class PatternsBasicWidget : public BasicWidget PatternEditor* editor; QPushButton* compileButton; QPushButton* addButton; - QPushButton* clearAllButton; + QPushButton* clearButton; + QPushButton* matchButton; + QPushButton* selectAllButton; + QPushButton* deselectAllButton; PatternCompiler* comp; PatternsList* list; void initPatternEditor(); @@ -46,10 +49,14 @@ class PatternsBasicWidget : public BasicWidget QStringList getPatterns() const; void loadPatterns(const QStringList& patterns); signals: - + void matchClicked(); public slots: void slotClearPatterns(); void slotAddPattern(); + void slotEnableMatch(); + void slotMatchClicked(); + void slotDisableMatch(); + void slotDeleteSelected(); }; #endif // PATTERNSBASICWIDGET_H diff --git a/Patterns/patternslist.cpp b/Patterns/patternslist.cpp index 68e6d84..2311967 100644 --- a/Patterns/patternslist.cpp +++ b/Patterns/patternslist.cpp @@ -17,17 +17,14 @@ PatternsList::PatternsList(PatternCompiler* compiler,QWidget *parent):QTableView // setResizeMode(QListView::Adjust); setContextMenuPolicy(Qt::ActionsContextMenu); removeAction = new QAction("Удалить",this); - editAction = new QAction("Изменить", this); this->horizontalHeader()->setStretchLastSection(true); this->verticalHeader()->hide(); this->horizontalHeader()->hide(); this->setColumnWidth(PatternListModel::CHECK_COLUMN,10); addAction(removeAction); - addAction(editAction); this->resizeColumnsToContents(); this->resizeRowsToContents(); connect(removeAction,SIGNAL(triggered()),this,SLOT(slotRemovePattern())); - connect(editAction,SIGNAL(triggered()),this,SLOT(slotEditPattern())); connect(delegate,SIGNAL(textUpdated()),this,SLOT(slotTextEntered())); } @@ -109,20 +106,26 @@ void PatternsList::slotRemovePattern() } } -void PatternsList::slotEditPattern() +void PatternsList::slotTextEntered() { - QModelIndexList indexes = selectionModel()->selectedIndexes(); - if(!indexes.isEmpty()) { - QModelIndex ind = indexes[0]; - PatternListModel::ListItem dat = qvariant_cast(ind.data(Qt::DisplayRole)); - emit editPatternSignal(dat.name+" = "+dat.text); - myModel->removeRows(ind.row(),1,ind); + resize(this->width(),this->height()+1); +} + +void PatternsList::slotDeleteChecked() +{ + QVector selRows = myModel->getSelectedPatterns(); + for (int i = 0; i < selRows.size(); ++i){ + myModel->removeRows(selRows[i],1,QModelIndex()); } } +void PatternsList::slotSelectAll() +{ + myModel->checkAll(); +} -void PatternsList::slotTextEntered() +void PatternsList::slotDeselectAll() { - resize(this->width(),this->height()+1); + myModel->resetAll(); } diff --git a/Patterns/patternslist.h b/Patterns/patternslist.h index 512731c..ea043d2 100644 --- a/Patterns/patternslist.h +++ b/Patterns/patternslist.h @@ -22,6 +22,7 @@ class PatternsList : public QTableView void clearAll(); void addPattern(const QString& text); void addPatterns(const QStringList& patterns); + QStringList getSelectedPatternNames() const {return myModel->getSelectedNames();} protected: void resizeEvent(QResizeEvent *e) Q_DECL_OVERRIDE; private: @@ -38,8 +39,10 @@ class PatternsList : public QTableView public slots: void slotCompilePatterns(); void slotRemovePattern(); - void slotEditPattern(); void slotTextEntered(); + void slotDeleteChecked(); + void slotSelectAll(); + void slotDeselectAll(); signals: void editPatternSignal(const QString& text); }; diff --git a/Text/matchtextviewer.cpp b/Text/matchtextviewer.cpp index 3684dbd..dde65fe 100644 --- a/Text/matchtextviewer.cpp +++ b/Text/matchtextviewer.cpp @@ -106,7 +106,6 @@ bool MatchTextViewer::isModified() void MatchTextViewer::dehighlightAll() { setExtraSelections(QList()); - emit jobDone(); } diff --git a/Text/textbasicwidget.cpp b/Text/textbasicwidget.cpp index 0180fce..0d4d7d2 100644 --- a/Text/textbasicwidget.cpp +++ b/Text/textbasicwidget.cpp @@ -110,11 +110,6 @@ void TextBasicWidget::slotClearMatches() textEdit->clearSelection(); } -void TextBasicWidget::slotAnalyzeText(){ - if(textEdit->count() > 0){ - emit buttonClicked(); - } -} void TextBasicWidget::slotEditEnable() { @@ -167,13 +162,10 @@ void TextBasicWidget::initButtons(QMainWindow* wrapper) QToolBar* buttonBar = new QToolBar(this); buttonBar->setMovable(false); buttonBar->setFloatable(false); - analyze = new QPushButton(tr("Наложить"),this); - edit = new QPushButton(tr("Редактировать"),this); + edit = new QPushButton(tr("Редактирование"),this); statistics = new QPushButton(tr("Статистика"),this); - connect(analyze,SIGNAL(clicked(bool)),this,SLOT(slotAnalyzeText())); connect(edit,SIGNAL(clicked(bool)),this,SLOT(slotEditEnable())); connect(statistics,SIGNAL(clicked(bool)),this,SLOT(slotShowStatistics())); - buttonBar->addWidget(analyze); buttonBar->addWidget(edit); buttonBar->addWidget(statistics); wrapper->addToolBar(buttonBar); diff --git a/Text/textbasicwidget.h b/Text/textbasicwidget.h index fbf015e..c4a38b4 100644 --- a/Text/textbasicwidget.h +++ b/Text/textbasicwidget.h @@ -51,14 +51,12 @@ class TextBasicWidget : public BasicWidget QString getText() const ; signals: - void buttonClicked(); void editEnabled(); void tabClosed(int); void tabChanged(int); void checkingEnabled(); public slots: void slotClearMatches(); - void slotAnalyzeText(); void slotEditEnable(); void slotShowStatistics(); void slotSelectFragment(int from,int to); diff --git a/centralwidget.cpp b/centralwidget.cpp index 6042098..af795ac 100644 --- a/centralwidget.cpp +++ b/centralwidget.cpp @@ -33,10 +33,11 @@ CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), timeOut(DEFAULT // vert->addWidget(progress); mainLay->addWidget(vert); setLayout(mainLay); - connect(txt,SIGNAL(buttonClicked()),this,SLOT(slotAnalyze())); + connect(pattern,SIGNAL(matchClicked()), this,SLOT(slotAnalyze())); connect(matches,SIGNAL(patternWasUnchecked(QString)),txt,SLOT(slotPatternUncheked(QString))); connect(matches,SIGNAL(patternWasChecked(QString)),txt,SLOT(slotPatternChecked(QString))); connect(matches,SIGNAL(showAll()),txt,SLOT(slotHighlightAll())); + connect(matches,SIGNAL(showAll()),pattern,SLOT(slotDisableMatch())); connect(matches,SIGNAL(hideAll()),txt,SLOT(slotDehighlightAll())); connect(matches,SIGNAL(rowClicked(int,int)),txt,SLOT(slotSelectFragment(int,int))); connect(matches,SIGNAL(patternWasUnchecked(QString)),this,SIGNAL(statusHighlighting())); @@ -48,6 +49,7 @@ CentralWidget::CentralWidget(QWidget *parent) : QWidget(parent), timeOut(DEFAULT connect(txt,SIGNAL(editEnabled()),this,SLOT(slotEdit())); connect(txt,SIGNAL(checkingEnabled()),matches,SLOT(slotEnableChecking())); connect(txt,SIGNAL(checkingEnabled()),this,SIGNAL(statusReady())); + connect(txt, SIGNAL(checkingEnabled()),pattern, SLOT(slotEnableMatch())); connect(watcher,SIGNAL(finished()),this,SLOT(slotDisplay())); connect(timeoutTimer,SIGNAL(timeout()),this,SLOT(slotTimeout())); @@ -115,12 +117,14 @@ void CentralWidget::createBackup() const void CentralWidget::slotAnalyze() { QStringList patternNames = pattern->getChoosenPatternsNames(); - if(!patternNames.isEmpty()){ + if(!patternNames.isEmpty() && !txt->getText().isEmpty()){ emit statusEngine(); txt->setReadOnly(true); QFuture> future = QtConcurrent::run(compiler,&PatternCompiler::analyzeText,patternNames,txt->getText()); watcher->setFuture(future); timeoutTimer->start(timeOut); + } else { + pattern->slotEnableMatch(); } }