| 1 |
#include "mainwindow.h" |
| 2 |
#include "ui_mainwindow.h" |
| 3 |
#include <polynomialsolver.h> |
| 4 |
#include <QDebug> |
| 5 |
#include <QPainter> |
| 6 |
#include <QMessageBox> |
| 7 |
#include <QFileDialog> |
| 8 |
#include "polfileeditorwindow.h" |
| 9 |
|
| 10 |
using namespace xmpsolve; |
| 11 |
|
| 12 |
MainWindow::MainWindow(QWidget *parent) : |
| 13 |
QMainWindow(parent),ui(new Ui::MainWindow) |
| 14 |
{ |
| 15 |
ui->setupUi(this); |
| 16 |
|
| 17 |
// Connect the signal of computation finished to its callback in |
| 18 |
// here. |
| 19 |
QObject::connect(&m_solver, SIGNAL(solved()), |
| 20 |
this, SLOT(polynomial_solved())); |
| 21 |
|
| 22 |
ui->listRootsView->setModel(m_solver.rootsModel()); |
| 23 |
ui->graphicsView->setModel(m_solver.rootsModel()); |
| 24 |
|
| 25 |
m_polFileEditorWindow = NULL; |
| 26 |
|
| 27 |
// Synchronize the selection of the roots with the rootsView, so we can |
| 28 |
// focus the currently selected root. |
| 29 |
QObject::connect(ui->listRootsView->selectionModel(), |
| 30 |
SIGNAL(selectionChanged(QItemSelection,QItemSelection)), |
| 31 |
this, |
| 32 |
SLOT(onlistRootsView_selectionChanged(QItemSelection,QItemSelection))); |
| 33 |
} |
| 34 |
|
| 35 |
MainWindow::~MainWindow() |
| 36 |
{ |
| 37 |
if (m_polFileEditorWindow != NULL) |
| 38 |
delete m_polFileEditorWindow; |
| 39 |
delete ui; |
| 40 |
} |
| 41 |
|
| 42 |
void |
| 43 |
MainWindow::openEditor(QString polFile) |
| 44 |
{ |
| 45 |
if (m_polFileEditorWindow == NULL) { |
| 46 |
m_polFileEditorWindow = new PolFileEditorWindow(); |
| 47 |
|
| 48 |
// Solve .pol files requested by the user |
| 49 |
QObject::connect(m_polFileEditorWindow, SIGNAL(solvePoly(QString)), |
| 50 |
this, SLOT(onSolvePolFileRequested(QString))); |
| 51 |
|
| 52 |
QObject::connect(m_polFileEditorWindow, SIGNAL(destroyed()), |
| 53 |
this, SLOT(onPolFileEditorWindowDestroyed())); |
| 54 |
} |
| 55 |
|
| 56 |
if (! polFile.isEmpty()) { |
| 57 |
m_polFileEditorWindow->loadPolFile(polFile); |
| 58 |
} |
| 59 |
|
| 60 |
m_polFileEditorWindow->raise(); |
| 61 |
m_polFileEditorWindow->show(); |
| 62 |
} |
| 63 |
|
| 64 |
void |
| 65 |
MainWindow::onPolFileEditorWindowDestroyed() |
| 66 |
{ |
| 67 |
m_polFileEditorWindow->deleteLater(); |
| 68 |
m_polFileEditorWindow = NULL; |
| 69 |
} |
| 70 |
|
| 71 |
void |
| 72 |
MainWindow::lockInterface() |
| 73 |
{ |
| 74 |
ui->solveButton->setEnabled(false); |
| 75 |
} |
| 76 |
|
| 77 |
void |
| 78 |
MainWindow::unlockInterface() |
| 79 |
{ |
| 80 |
ui->solveButton->setEnabled(true); |
| 81 |
} |
| 82 |
|
| 83 |
int MainWindow::requiredDigits() |
| 84 |
{ |
| 85 |
return ui->digitsSpinBox->value(); |
| 86 |
} |
| 87 |
|
| 88 |
mps_algorithm MainWindow::selectedAlgorithm() |
| 89 |
{ |
| 90 |
mps_algorithm selected_algorithm = (ui->algorithmComboBox->currentIndex() == 0) ? |
| 91 |
MPS_ALGORITHM_SECULAR_GA : MPS_ALGORITHM_STANDARD_MPSOLVE; |
| 92 |
return selected_algorithm; |
| 93 |
} |
| 94 |
|
| 95 |
PolynomialBasis MainWindow::polynomialBasis() |
| 96 |
{ |
| 97 |
PolynomialBasis basis = MONOMIAL; |
| 98 |
switch (ui->basisComboBox->currentIndex()) { |
| 99 |
case 0: |
| 100 |
basis = MONOMIAL; |
| 101 |
break; |
| 102 |
case 1: |
| 103 |
basis = CHEBYSHEV; |
| 104 |
break; |
| 105 |
} |
| 106 |
|
| 107 |
return basis; |
| 108 |
} |
| 109 |
|
| 110 |
mps_output_goal MainWindow::selectedGoal() |
| 111 |
{ |
| 112 |
switch (ui->goalComboBox->currentIndex()) { |
| 113 |
case 0: |
| 114 |
return MPS_OUTPUT_GOAL_ISOLATE; |
| 115 |
break; |
| 116 |
|
| 117 |
case 1: |
| 118 |
return MPS_OUTPUT_GOAL_APPROXIMATE; |
| 119 |
break; |
| 120 |
default: |
| 121 |
// This is kept for now as a default value, but this |
| 122 |
// point should not be reached. |
| 123 |
return MPS_OUTPUT_GOAL_APPROXIMATE; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
void MainWindow::on_solveButton_clicked() |
| 128 |
{ |
| 129 |
lockInterface(); |
| 130 |
|
| 131 |
ui->statusBar->showMessage(tr("Solving polynomial...")); |
| 132 |
if (m_solver.solvePoly(ui->polyLineEdit->toPlainText(), polynomialBasis(), |
| 133 |
selectedAlgorithm(), requiredDigits(), |
| 134 |
selectedGoal()) < 0) |
| 135 |
{ |
| 136 |
ui->statusBar->showMessage(tr("Polynomial parsing failed")); |
| 137 |
QMessageBox mbox(QMessageBox::Critical, tr("Error while parsing the polynomial"), |
| 138 |
tr("The parser reported the following error: ") + |
| 139 |
m_solver.errorMessage(), QMessageBox::Ok); |
| 140 |
mbox.exec(); |
| 141 |
unlockInterface(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
void |
| 146 |
MainWindow::polynomial_solved() |
| 147 |
{ |
| 148 |
ui->statusBar->showMessage(tr("Polynomial solved in %1ms").arg(m_solver.CPUTime())); |
| 149 |
|
| 150 |
unlockInterface(); |
| 151 |
} |
| 152 |
|
| 153 |
void xmpsolve::MainWindow::openPolFile(QString path) |
| 154 |
{ |
| 155 |
openEditor(path); |
| 156 |
} |
| 157 |
|
| 158 |
void xmpsolve::MainWindow::onSolvePolFileRequested(QString content) |
| 159 |
{ |
| 160 |
lockInterface(); |
| 161 |
|
| 162 |
if (m_solver.solvePolFileFromContent(content, selectedAlgorithm(), |
| 163 |
requiredDigits(), selectedGoal()) < 0) { |
| 164 |
ui->statusBar->showMessage(tr("Polynomial parsing failed")); |
| 165 |
|
| 166 |
QMessageBox::critical(this, |
| 167 |
tr("Error while parsing the polynomial"), |
| 168 |
tr("The parser reported the following error: ") + |
| 169 |
m_solver.errorMessage(), QMessageBox::Ok); |
| 170 |
|
| 171 |
unlockInterface(); |
| 172 |
} |
| 173 |
|
| 174 |
// Grab focus so we can display the result. |
| 175 |
raise(); |
| 176 |
} |
| 177 |
|
| 178 |
void xmpsolve::MainWindow::on_listRootsView_clicked(const QModelIndex &index) |
| 179 |
{ |
| 180 |
ui->approximationDetailLabel->setText( |
| 181 |
m_solver.rootsModel()->data(index, RootsModel::SHORT_APPROXIMATION).toString()); |
| 182 |
ui->approximationRadiusLabel->setText( |
| 183 |
m_solver.rootsModel()->data(index, RootsModel::RADIUS).toString()); |
| 184 |
ui->approximationStatusLabel->setText( |
| 185 |
m_solver.rootsModel()->data(index, RootsModel::STATUS).toString()); |
| 186 |
} |
| 187 |
|
| 188 |
void xmpsolve::MainWindow::onlistRootsView_selectionChanged(QItemSelection,QItemSelection) |
| 189 |
{ |
| 190 |
QModelIndexList indexes = ui->listRootsView->selectionModel()->selection().indexes(); |
| 191 |
|
| 192 |
switch (indexes.length()) |
| 193 |
{ |
| 194 |
case 0: |
| 195 |
m_solver.rootsModel()->markRoot(); |
| 196 |
break; |
| 197 |
case 1: |
| 198 |
m_solver.rootsModel()->markRoot(indexes.at(0).row()); |
| 199 |
break; |
| 200 |
default: |
| 201 |
qDebug() << "More than 1 index selected"; |
| 202 |
break; |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
void xmpsolve::MainWindow::on_actionOpen_pol_file_triggered() |
| 207 |
{ |
| 208 |
// If the user click on open polFile, we need to check that |
| 209 |
// he has selected a valid .pol file and - if that's the |
| 210 |
// case, solve the associated polynomial. |
| 211 |
QString selectedFile = QFileDialog::getOpenFileName(this, |
| 212 |
tr("Select .pol file"), |
| 213 |
QString(), |
| 214 |
"Pol files (*.pol);;Text files (*.txt)"); |
| 215 |
openPolFile(selectedFile); |
| 216 |
} |
| 217 |
|
| 218 |
void xmpsolve::MainWindow::closeEvent(QCloseEvent *) |
| 219 |
{ |
| 220 |
QApplication::exit(); |
| 221 |
} |
| 222 |
|
| 223 |
void xmpsolve::MainWindow::on_actionQuit_triggered() |
| 224 |
{ |
| 225 |
QApplication::exit(); |
| 226 |
} |
| 227 |
|
| 228 |
void xmpsolve::MainWindow::on_actionAbout_MPSolve_triggered() |
| 229 |
{ |
| 230 |
QMessageBox::about(this, tr("About ") + PACKAGE_STRING, |
| 231 |
QString("<h1>%1</h1>").arg(PACKAGE_STRING) + |
| 232 |
"MPSolve is free software released under the GNU General Public License 3.<br>" + |
| 233 |
"Further documentation and a bug tracker are available at our " + |
| 234 |
"<a href=\"http://mpsolve.dm.unipi.it/mpsolve/\">website</a>. " + |
| 235 |
"<h3>Authors:</h3>" + |
| 236 |
" - Dario A. Bini <<a href=\"mailto:[email protected]\">[email protected]</a>><br>" + |
| 237 |
" - Giuseppe Fiorentino <<a href=\"mailto:[email protected]\">[email protected]</a>><br>" + |
| 238 |
" - Leonardo Robol <<a href=\"mailto:[email protected]\">[email protected]</a>> <br>"); |
| 239 |
} |
| 240 |
|
| 241 |
void xmpsolve::MainWindow::on_actionOpen_editor_triggered() |
| 242 |
{ |
| 243 |
openEditor(); |
| 244 |
} |
| 245 |
|
| 246 |
void xmpsolve::MainWindow::on_actionAbort_computations_triggered() |
| 247 |
{ |
| 248 |
m_solver.abortComputations(); |
| 249 |
ui->statusBar->showMessage(tr("Waiting for MPSolve to complete the current operation...")); |
| 250 |
} |
| 251 |
|
| 252 |
void xmpsolve::MainWindow::on_openPolFileButton_clicked() |
| 253 |
{ |
| 254 |
on_actionOpen_pol_file_triggered(); |
| 255 |
} |
| 256 |
|
| 257 |
void xmpsolve::MainWindow::on_zoomInButton_clicked() |
| 258 |
{ |
| 259 |
ui->graphicsView->zoomIn(); |
| 260 |
} |
| 261 |
|
| 262 |
void xmpsolve::MainWindow::on_zoomOutButton_clicked() |
| 263 |
{ |
| 264 |
ui->graphicsView->zoomOut(); |
| 265 |
} |