00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <MainWindow.hpp>
00021
00022 #include <QtGui/QCloseEvent>
00023 #include <QtGui/QImage>
00024 #include <QtGui/QPixmap>
00025 #include <QtGui/QToolBar>
00026 #include <QtGui/QToolButton>
00027 #include <QtGui/QMenu>
00028 #include <QtGui/QMenuBar>
00029
00030 #include <QtGui/QPushButton>
00031 #include <QtGui/QApplication>
00032 #include <QtGui/QSlider>
00033 #include <QtGui/QGridLayout>
00034
00035 #include <QtGui/QLabel>
00036 #include <QtGui/QGroupBox>
00037 #include <QtGui/QLayout>
00038
00039 #include <QtGui/QTabWidget>
00040 #include <QtGui/QFileDialog>
00041 #include <QtCore/QFile>
00042 #include <QtCore/QTextStream>
00043 #include <QtGui/QMainWindow>
00044 #include <QtGui/QStatusBar>
00045
00046 #include <QtGui/QMessageBox>
00047 #include <QtGui/QPrinter>
00048 #include <QtGui/QApplication>
00049
00050 #include <QtGui/QPainter>
00051 #include <QtGui/QWhatsThis>
00052 #include <QtGui/QAction>
00053
00054 #include <QtGui/QDockWidget>
00055
00056 #include <filesave.xpm>
00057 #include <fileopen.xpm>
00058 #include <fileprint.xpm>
00059
00060 #include <QtCore/QTimer>
00061 #include <QVTKWidget.h>
00062
00063 #include <vtkRenderer.h>
00064 #include <vtkRenderWindow.h>
00065
00066 #include <vtkAxes.h>
00067 #include <vtkVectorText.h>
00068 #include <vtkTextSource.h>
00069 #include <vtkPolyDataMapper.h>
00070 #include <vtkLODActor.h>
00071
00072 #include <EditorWindow.hpp>
00073
00074 #include <FFThread.hpp>
00075 #include <StreamCenter.hpp>
00076
00077 #include <sstream>
00078
00079 #include <QtGui/QTextCharFormat>
00080
00081 void MainWindow::__buildFileTools()
00082 {
00083 QAction * fileNewAction;
00084 QAction * fileOpenAction;
00085 QAction * fileSaveAction, * fileSaveAsAction, * filePrintAction;
00086 QAction * fileCloseAction, * fileQuitAction;
00087
00088 fileNewAction = new QAction("&New", this);
00089 connect(fileNewAction, SIGNAL(activated()), this,
00090 SLOT(newFile()));
00091 fileNewAction->setShortcut(tr("Ctrl+N"));
00092
00093 fileOpenAction = new QAction(QPixmap(fileopen), "&Open...", this);
00094 connect(fileOpenAction, SIGNAL(activated()), this, SLOT(choose()));
00095
00096 fileOpenAction->setShortcut(tr("Ctrl+O"));
00097 const char * fileOpenText =
00098 "<p><img source=\"fileopen\"> "
00099 "Click this button to open a <it>new file</it>. <br>"
00100 "You can also select the <b>Open</b> command "
00101 "from the <b>File</b> menu.</p>";
00102
00103
00104 fileOpenAction->setWhatsThis(fileOpenText);
00105
00106 fileSaveAction = new QAction(QPixmap(filesave), "&Save", this);
00107 connect(fileSaveAction, SIGNAL(activated()), this, SLOT(save()));
00108
00109 fileSaveAction->setShortcut(tr("Ctrl+S"));
00110 const char * fileSaveText =
00111 "<p>Click this button to save the file you "
00112 "are editing. You will be prompted for a file name.\n"
00113 "You can also select the <b>Save</b> command "
00114 "from the <b>File</b> menu.</p>";
00115 fileSaveAction->setWhatsThis(fileSaveText);
00116
00117 fileSaveAsAction = new QAction(tr("Save &As..."), this);
00118 connect(fileSaveAsAction, SIGNAL(activated()), this,
00119 SLOT(saveAs()));
00120 fileSaveAsAction->setWhatsThis(fileSaveText);
00121
00122 filePrintAction = new QAction(QPixmap(fileprint), tr("&Print..."), this);
00123 connect(filePrintAction, SIGNAL(activated()), this,
00124 SLOT(print()));
00125 filePrintAction->setShortcut(tr("Ctrl+P"));
00126
00127 const char * filePrintText = "Click this button to print the file you "
00128 "are editing.\n You can also select the Print "
00129 "command from the File menu.";
00130 filePrintAction->setWhatsThis(filePrintText);
00131
00132 fileCloseAction = new QAction("&Close", this);
00133 connect(fileCloseAction, SIGNAL(activated()), this,
00134 SLOT(close()));
00135 fileCloseAction->setShortcut(tr("Ctrl+W"));
00136
00137 fileQuitAction = new QAction(tr("&Quit"), this);
00138 connect(fileQuitAction, SIGNAL(activated()), qApp,
00139 SLOT(closeAllWindows()));
00140 fileQuitAction->setShortcut(tr("Ctrl+Q"));
00141
00142
00143
00144 QToolBar * fileTools = addToolBar(tr("File operations"));
00145 fileTools->addAction(fileOpenAction);
00146 fileTools->addAction(fileSaveAction);
00147 fileTools->addAction(filePrintAction);
00148
00149
00150
00151
00152
00153
00154 QMenu * file = new QMenu(tr("&File"), this);
00155 menuBar()->addMenu(file);
00156 file->addAction(fileNewAction);
00157 file->addAction(fileOpenAction);
00158 file->addAction(fileSaveAction);
00159 file->addAction(fileSaveAsAction);
00160 file->addSeparator();
00161 file->addAction(filePrintAction);
00162 file->addSeparator();
00163 file->addAction(fileCloseAction);
00164 file->addAction(fileQuitAction);
00165 }
00166
00167 void MainWindow::__buildEditorTools()
00168 {
00169 QMenu * editorMenu = new QMenu(tr("&Editor"), this);
00170 menuBar()->addMenu(editorMenu);
00171 editorMenu->addAction(tr("&Undo"), __editor, SLOT(undo()), tr("Ctrl+Z"));
00172 editorMenu->addAction(tr("&Redo"), __editor, SLOT(redo()), tr("Ctrl+Y"));
00173 editorMenu->addSeparator();
00174 editorMenu->addAction(tr("Cu&t"), __editor, SLOT(cut()), tr("Ctrl+X"));
00175 editorMenu->addAction(tr("&Copy"), __editor, SLOT(copy()), tr("Ctrl+C"));
00176 editorMenu->addAction(tr("&Paste"),__editor, SLOT(paste()),tr("Ctrl+V"));
00177 editorMenu->addSeparator();
00178 editorMenu->addAction(tr("Select All"), __editor, SLOT(selectAll()));
00179 editorMenu->addSeparator();
00180 editorMenu->addAction(tr("Change &Font"),__editor, SLOT(fontDialogue()));
00181
00182
00183 }
00184
00185
00186 void MainWindow::__buildActionTools()
00187 {
00188
00189 QMenu * action = new QMenu("&Action", this);
00190 menuBar()->addMenu(action);
00191 action->addAction(tr("&Run"), this, SLOT(run()),
00192 tr("Ctrl+R"));
00193 action->addAction(tr("&Pause"), this, SLOT(pause()));
00194 action->addAction(tr("&Resume"), this, SLOT(resume()));
00195 action->addAction(tr("&Stop"), this, SLOT(stop()));
00196 }
00197
00198 void MainWindow::__buildHelpTools()
00199 {
00200 QMenu * help = new QMenu(tr("&Help"), this);
00201 menuBar()->addMenu(help);
00202 help->addAction(tr("&About"), this, SLOT(aboutff3d()),
00203 tr("F1"));
00204 help->addAction(tr("About &Qt"), this, SLOT(aboutQt()));
00205 help->addAction(tr("About &Vtk"), this, SLOT(aboutVtk()));
00206 help->addSeparator();
00207 help->addAction(tr("What's &This"), this, SLOT(whatsThis()),
00208 tr("Shift+F1"));
00209 }
00210
00211 MainWindow::MainWindow()
00212 : QMainWindow(0),
00213 __ffThread(0)
00214 {
00215 printer = new QPrinter(QPrinter::HighResolution);
00216
00217
00218 QTabWidget* tabWidget = new QTabWidget(this);
00219
00220
00221 __editor = new EditorWindow;
00222 __editor->setFocus();
00223
00224 setCentralWidget(tabWidget);
00225
00226 tabWidget->addTab(__editor,tr("Editor"));
00227
00228 QVTKWidget* graphicWindow = new QVTKWidget;
00229 tabWidget->addTab(graphicWindow, tr("Graphics"));
00230
00231 vtkRenderer *ren1= vtkRenderer::New();
00232 ren1->SetBackground( 0.1, 0.2, 0.4 );
00233 vtkVectorText* text = vtkVectorText::New();
00234 text->SetText("ff3d");
00235
00236 vtkPolyDataMapper* textMapper = vtkPolyDataMapper::New();
00237 textMapper->SetInput(text->GetOutput());
00238 vtkLODActor *actor = vtkLODActor::New();
00239 actor->SetMapper(textMapper);
00240 ren1->AddActor( actor );
00241
00242 graphicWindow->GetRenderWindow()->AddRenderer(ren1);
00243 graphicWindow->GetRenderWindow()->Render();
00244
00245
00246 QDockWidget* consoleDock = new QDockWidget("console", this);
00247 __console = new QTextEdit;
00248 __console->setReadOnly(true);
00249 __console->setUndoRedoEnabled(false);
00250 consoleDock->setWidget(__console);
00251 this->addDockWidget(Qt::BottomDockWidgetArea, consoleDock);
00252
00253
00254 this->__buildFileTools();
00255 this->__buildEditorTools();
00256 this->__buildActionTools();
00257
00258 menuBar()->addSeparator();
00259
00260 this->__buildHelpTools();
00261
00262 statusBar()->showMessage(tr("Wellcome to FreeFEM3D"), 2000);
00263 }
00264
00265
00266 MainWindow::~MainWindow()
00267 {
00268 if (__ffThread !=0) {
00269 delete __ffThread;
00270 }
00271 delete printer;
00272 }
00273
00274
00275
00276 void MainWindow::newFile()
00277 {
00278 MainWindow *ed = new MainWindow;
00279 ed->show();
00280 }
00281
00282 void MainWindow::choose()
00283 {
00284 filename = QFileDialog::getOpenFileName(this);
00285 if (!filename.isEmpty())
00286 load(filename);
00287 else
00288 statusBar()->showMessage("Loading aborted", 2000);
00289 }
00290
00291
00292 void MainWindow::load(const QString &fileName)
00293 {
00294 QFile f(fileName);
00295 if (!f.open( QIODevice::ReadOnly))
00296 return;
00297
00298 QTextStream ts(&f);
00299 __editor->setPlainText(ts.readAll());
00300 __editor->setWindowModified(FALSE);
00301 setWindowTitle(fileName);
00302 statusBar()->showMessage("Loaded document " + fileName, 2000);
00303 }
00304
00305
00306 void MainWindow::save()
00307 {
00308 if (filename.isEmpty()) {
00309 saveAs();
00310 return;
00311 }
00312
00313 const QString& text = __editor->toPlainText();
00314 QFile f(filename);
00315 if (!f.open(QIODevice::WriteOnly)) {
00316 statusBar()->showMessage(QString("Could not write to %1").arg(filename),
00317 2000);
00318 return;
00319 }
00320
00321 QTextStream t(&f);
00322 t << text;
00323 f.close();
00324
00325 __editor->setWindowModified(FALSE);
00326
00327 setWindowTitle(filename);
00328
00329 statusBar()->showMessage(QString("File %1 saved").arg(filename), 2000);
00330 }
00331
00332
00333 void MainWindow::saveAs()
00334 {
00335 QString fn = QFileDialog::getSaveFileName(this);
00336 if (!fn.isEmpty()) {
00337 filename = fn;
00338 save();
00339 } else {
00340 statusBar()->showMessage("Saving aborted", 2000);
00341 }
00342 }
00343
00344
00345 void MainWindow::print()
00346 {
00347 printer->setFullPage(TRUE);
00348 statusBar()->showMessage("Printing...");
00349 QPainter p;
00350 if(!p.begin(printer)) {
00351 statusBar()->showMessage("Printing aborted", 2000);
00352 return;
00353 }
00354 fferr(1) << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384 }
00385
00386 void MainWindow::closeEvent(QCloseEvent* ce)
00387 {
00388 if (!__editor->isWindowModified()) {
00389 ce->accept();
00390 return;
00391 }
00392
00393 switch(QMessageBox::information(this, "FreeFEM3D",
00394 "The document has been changed since "
00395 "the last save.",
00396 "Save Now", "Cancel", "Leave Anyway",
00397 0, 1)) {
00398 case 0:
00399 save();
00400 ce->accept();
00401 break;
00402 case 1:
00403 default:
00404 ce->ignore();
00405 break;
00406 case 2:
00407 ce->accept();
00408 break;
00409 }
00410 }
00411
00412
00413 void MainWindow::run()
00414 {
00415 std::stringstream text;
00416 text << __editor->toPlainText().toAscii().constData() << std::ends;
00417
00418 QTextCharFormat defaultFormat;
00419 defaultFormat.setFontFamily("Courier New");
00420 defaultFormat.setFontPointSize(8);
00421
00422 __console->mergeCurrentCharFormat(defaultFormat);
00423 __console->append("Running");
00424
00425 __ffThread =new FFThread;
00426 __ffThread->setInput(text);
00427 __ffThread->setConsole(__console);
00428 __ffThread->start();
00429
00430 statusBar()->showMessage("Running...", 2000);
00431 usleep(500000);
00432 }
00433
00434 void MainWindow::pause()
00435 {
00436 fferr(1) << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00437 }
00438 void MainWindow::resume()
00439 {
00440 fferr(1) << __FILE__ << ':' << __LINE__ << ": Not implemented\n";
00441 }
00442
00443 void MainWindow::stop()
00444 {
00445 if (__ffThread != 0) {
00446 __ffThread->terminate();
00447 delete __ffThread;
00448 }
00449 }
00450
00451
00452 void MainWindow::aboutff3d()
00453 {
00454 QMessageBox::about(this, "FreeFEM3D",
00455 "FreeFEM3D, blablabla");
00456 }
00457
00458
00459 void MainWindow::aboutQt()
00460 {
00461 QMessageBox::aboutQt(this, "Qt blabla");
00462 }
00463
00464 void MainWindow::aboutVtk()
00465 {
00466 QMessageBox::about(this, "Vtk", "Vtk blabla");
00467 }