Allow saving the results of a regular expression search into a file

This commit is contained in:
Olivier Teulière 2012-02-25 14:13:26 +01:00
parent 746abef2cf
commit cae5f38af5
3 changed files with 64 additions and 3 deletions

View file

@ -75,6 +75,8 @@ DicToolsWidget::DicToolsWidget(QWidget *parent)
this, SLOT(refreshRegexp()));
QObject::connect(buttonSearchRegexp, SIGNAL(clicked()),
this, SLOT(refreshRegexp()));
QObject::connect(buttonSaveRegexp, SIGNAL(clicked()),
this, SLOT(saveRegexpResults()));
QObject::connect(buttonSaveWords, SIGNAL(clicked()),
this, SLOT(exportWordsList()));
@ -244,7 +246,9 @@ void DicToolsWidget::refreshPlus1()
void DicToolsWidget::enableSearchRegexp()
{
buttonSearchRegexp->setEnabled(lineEditRegexp->text() != "");
bool hasText = lineEditRegexp->text() != "";
buttonSearchRegexp->setEnabled(hasText);
buttonSaveRegexp->setEnabled(m_dic != NULL && hasText);
}
@ -306,6 +310,47 @@ void DicToolsWidget::refreshRegexp()
}
void DicToolsWidget::saveRegexpResults()
{
if (m_dic == NULL)
return;
QString fileName = QFileDialog::getSaveFileName(this, _q("Save words list"));
if (fileName != "")
{
const wstring &input = m_dic->convertFromInput(wfq(lineEditRegexp->text().toUpper()));
unsigned lmin = spinBoxMinLength->value();
unsigned lmax = spinBoxMaxLength->value();
vector<wstring> wordList;
try
{
m_dic->searchRegExp(input, wordList, lmin, lmax, 0);
}
catch (InvalidRegexpException &e)
{
QMessageBox::warning(this, _q("Eliot - Error"),
_q("Invalid regular expression: %1").arg(qfl(e.what())));
return;
}
try
{
ofstream file(lfq(fileName).c_str());
foreach (const wstring &word, wordList)
{
file << lfw(word) << endl;
}
QMessageBox::information(this, _q("Save words list"),
_q("File '%1' successfully saved").arg(fileName));
}
catch (std::exception &e)
{
QMessageBox::warning(this, _q("Eliot - Error"),
_q("Cannot save the words list: %1").arg(e.what()));
}
}
}
void DicToolsWidget::refreshDicInfo()
{
if (m_dic == NULL)
@ -366,14 +411,14 @@ void DicToolsWidget::exportWordsList()
{
if (m_dic == NULL)
return;
QString fileName = QFileDialog::getSaveFileName(this, _q("Export words list"));
QString fileName = QFileDialog::getSaveFileName(this, _q("Save words list"));
if (fileName != "")
{
try
{
ofstream file(lfq(fileName).c_str());
ListDic::printWords(file, *m_dic);
QMessageBox::information(this, _q("Export words list"),
QMessageBox::information(this, _q("Save words list"),
_q("File '%1' successfully saved").arg(fileName));
}
catch (std::exception &e)

View file

@ -77,6 +77,8 @@ private slots:
void enableSearchPlus1();
void enableSearchRegexp();
void saveRegexpResults();
/// Force synchronizing the model with the "check" results
void refreshCheck();
/// Force synchronizing the model with the "plus 1" results

View file

@ -103,6 +103,16 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonSaveRegexp">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>_(&quot;Save list...&quot;)</string>
</property>
</widget>
</item>
</layout>
</item>
<item>
@ -122,6 +132,9 @@
<property name="maximum">
<number>15</number>
</property>
<property name="value">
<number>2</number>
</property>
</widget>
</item>
<item>
@ -287,6 +300,7 @@
<tabstop>treeViewPlus1</tabstop>
<tabstop>lineEditRegexp</tabstop>
<tabstop>buttonSearchRegexp</tabstop>
<tabstop>buttonSaveRegexp</tabstop>
<tabstop>spinBoxMinLength</tabstop>
<tabstop>spinBoxMaxLength</tabstop>
<tabstop>treeViewRegexp</tabstop>