CommandTest.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (c) 2002 Jürgen Riegel <juergen.riegel@web.de>              *
00003  *                                                                         *
00004  *   This file is part of the FreeCAD CAx development system.              *
00005  *                                                                         *
00006  *   This library is free software; you can redistribute it and/or         *
00007  *   modify it under the terms of the GNU Library General Public           *
00008  *   License as published by the Free Software Foundation; either          *
00009  *   version 2 of the License, or (at your option) any later version.      *
00010  *                                                                         *
00011  *   This library  is distributed in the hope that it will be useful,      *
00012  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00013  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00014  *   GNU Library General Public License for more details.                  *
00015  *                                                                         *
00016  *   You should have received a copy of the GNU Library General Public     *
00017  *   License along with this library; see the file COPYING.LIB. If not,    *
00018  *   write to the Free Software Foundation, Inc., 59 Temple Place,         *
00019  *   Suite 330, Boston, MA  02111-1307, USA                                *
00020  *                                                                         *
00021  ***************************************************************************/
00022 
00023 
00024 #include "PreCompiled.h"
00025 #ifndef _PreComp_
00026 # include <QApplication>
00027 # include <QEventLoop>
00028 # include <QFileDialog>
00029 # include <QMutex>
00030 # include <QThread>
00031 # include <QTimer>
00032 # include <QMdiArea>
00033 # include <QMdiSubWindow>
00034 # include <QWaitCondition>
00035 # include <QTranslator>
00036 #endif
00037 
00038 #include <Base/Console.h>
00039 #include "Application.h"
00040 #include "MainWindow.h"
00041 #include "MDIView.h"
00042 #include "Command.h"
00043 #include "Language/Translator.h"
00044 
00045 #include "ProgressBar.h"
00046 
00047 
00048 using namespace Gui;
00049 
00050 //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
00051 
00052 //===========================================================================
00053 // Std_TestQM
00054 //===========================================================================
00055 DEF_STD_CMD(Std_TestQM);
00056 
00057 Std_TestQM::Std_TestQM()
00058   : Command("Std_TestQM")
00059 {
00060     sGroup        = "Standard-Test";
00061     sMenuText     = "Test translation files...";
00062     sToolTipText  = "Test function to check .qm translation files";
00063     sWhatsThis    = sToolTipText;
00064     sStatusTip    = sToolTipText;
00065 }
00066 
00067 void Std_TestQM::activated(int iMsg)
00068 {
00069     QStringList files = QFileDialog::getOpenFileNames(getMainWindow(),
00070         QString::fromAscii("Test translation"), QString(),
00071         QString::fromAscii("Translation (*.qm)"));
00072     if (!files.empty()) {
00073         Translator::instance()->activateLanguage("English");
00074         QList<QTranslator*> i18n = qApp->findChildren<QTranslator*>();
00075         for (QList<QTranslator*>::Iterator it = i18n.begin(); it != i18n.end(); ++it)
00076             qApp->removeTranslator(*it);
00077         for (QStringList::Iterator it = files.begin(); it != files.end(); ++it) {
00078             QTranslator* translator = new QTranslator(qApp);
00079             if (translator->load(*it)) {
00080                 qApp->installTranslator(translator);
00081             }
00082             else {
00083                 delete translator;
00084             }
00085         }
00086     }
00087 }
00088 
00089 //===========================================================================
00090 // Std_Test1
00091 //===========================================================================
00092 DEF_STD_CMD_A(FCCmdTest1);
00093 
00094 FCCmdTest1::FCCmdTest1()
00095   : Command("Std_Test1")
00096 {
00097     sGroup        = "Standard-Test";
00098     sMenuText     = "Test1";
00099     sToolTipText  = "Test function 1";
00100     sWhatsThis    = sToolTipText;
00101     sStatusTip    = sToolTipText;
00102     sPixmap       = "Std_Tool1";
00103     sAccel        = "Ctrl+T";
00104 }
00105 
00106 void FCCmdTest1::activated(int iMsg)
00107 {
00108 
00109 }
00110 
00111 bool FCCmdTest1::isActive(void)
00112 {
00113     //return (GetActiveOCCDocument()!=NULL);
00114     return true;
00115 }
00116 
00117 //===========================================================================
00118 // Std_Test2
00119 //===========================================================================
00120 DEF_STD_CMD_A(FCCmdTest2);
00121 
00122 FCCmdTest2::FCCmdTest2()
00123   : Command("Std_Test2")
00124 {
00125     sGroup          = "Standard-Test";
00126     sMenuText       = "Test2";
00127     sToolTipText    = "Test function 2";
00128     sWhatsThis      = sToolTipText;
00129     sStatusTip      = sToolTipText;
00130     sPixmap         = "Std_Tool2";
00131 }
00132 
00133 
00134 void FCCmdTest2::activated(int iMsg)
00135 {
00136 
00137 
00138 }
00139 
00140 bool FCCmdTest2::isActive(void)
00141 {
00142     return (getDocument()!=NULL);
00143 }
00144 
00145 //===========================================================================
00146 // Std_Test3
00147 //===========================================================================
00148 DEF_STD_CMD_A(FCCmdTest3);
00149 
00150 FCCmdTest3::FCCmdTest3()
00151   : Command("Std_Test3")
00152 {
00153     sGroup          = "Standard-Test";
00154     sMenuText       = "Test3";
00155     sToolTipText    = "Test function 3";
00156     sWhatsThis      = sToolTipText;
00157     sStatusTip      = sToolTipText;
00158     sPixmap         = "Std_Tool3";
00159 }
00160 
00161 void FCCmdTest3::activated(int iMsg)
00162 {
00163     App::Document *pcDoc = getDocument();
00164     if (!pcDoc) return;
00165 }
00166 
00167 
00168 bool FCCmdTest3::isActive(void)
00169 {
00170     return (getDocument()!=NULL);
00171 }
00172 
00173 //===========================================================================
00174 // Std_Test4
00175 //===========================================================================
00176 
00177 DEF_STD_CMD_A(FCCmdTest4);
00178 
00179 FCCmdTest4::FCCmdTest4()
00180   : Command("Std_Test4")
00181 {
00182     sGroup          = "Standard-Test";
00183     sMenuText       = "Test4";
00184     sToolTipText    = "Test function 4";
00185     sWhatsThis      = sToolTipText;
00186     sStatusTip      = sToolTipText;
00187     sPixmap         = "Std_Tool4";
00188 }
00189 
00190 void FCCmdTest4::activated(int iMsg)
00191 {
00192     App::Document *pcDoc = getDocument();
00193     if(!pcDoc) return;
00194 }
00195 
00196 
00197 bool FCCmdTest4::isActive(void)
00198 {
00199     return (getDocument()!=NULL);
00200 }
00201 
00202 //===========================================================================
00203 // Std_Test5
00204 //===========================================================================
00205 DEF_STD_CMD_A(FCCmdTest5);
00206 
00207 FCCmdTest5::FCCmdTest5()
00208   : Command("Std_Test5")
00209 {
00210     sGroup          = "Standard-Test";
00211     sMenuText       = "Test5";
00212     sToolTipText    = "Test function 5";
00213     sWhatsThis      = sToolTipText;
00214     sStatusTip      = sToolTipText;
00215     sPixmap         = "Std_Tool5";
00216 }
00217 
00218 void FCCmdTest5::activated(int iMsg)
00219 {
00220     App::Document *pcDoc = getDocument();
00221     if(!pcDoc) return;
00222 }
00223 
00224 bool FCCmdTest5::isActive(void)
00225 {
00226   return (getDocument()!=NULL);
00227 }
00228 
00229 
00230 //===========================================================================
00231 // Std_Test6
00232 //===========================================================================
00233 DEF_STD_CMD_A(FCCmdTest6);
00234 
00235 FCCmdTest6::FCCmdTest6()
00236   : Command("Std_Test6")
00237 {
00238     sGroup          = "Standard-Test";
00239     sMenuText       = "Test6";
00240     sToolTipText    = "Test function 6";
00241     sWhatsThis      = sToolTipText;
00242     sStatusTip      = sToolTipText;
00243     sPixmap         = "Std_Tool6";
00244 }
00245 
00246 void FCCmdTest6::activated(int iMsg)
00247 {
00248     App::Document *pcDoc = getDocument();
00249     if(!pcDoc) return;
00250 }
00251 
00252 bool FCCmdTest6::isActive(void)
00253 {
00254     return (getDocument()!=NULL);
00255 }
00256 
00257 //===========================================================================
00258 // Std_TestProgress1
00259 //===========================================================================
00260 DEF_STD_CMD_A(CmdTestProgress1);
00261 
00262 CmdTestProgress1::CmdTestProgress1()
00263   : Command("Std_TestProgress1")
00264 {
00265     sGroup          = "Standard-Test";
00266     sMenuText       = "Breakable bar";
00267     sToolTipText    = "Test a breakable progress bar";
00268     sWhatsThis      = sToolTipText;
00269     sStatusTip      = sToolTipText;
00270     sPixmap         = "Std_Tool7";
00271 }
00272 
00273 void CmdTestProgress1::activated(int iMsg)
00274 {
00275     try
00276     {
00277         QMutex mutex;
00278         mutex.lock();
00279         unsigned long steps = 1000;
00280         Base::SequencerLauncher seq("Starting progress bar", steps);
00281 
00282         for (unsigned long i=0; i<steps;i++)
00283         {
00284             seq.next(true);
00285             QWaitCondition().wait(&mutex, 30);
00286         }
00287 
00288         mutex.unlock();
00289     }
00290     catch (...)
00291     {
00292     }
00293 }
00294 
00295 bool CmdTestProgress1::isActive(void)
00296 {
00297     return (!Base::Sequencer().isRunning());
00298 }
00299 
00300 //===========================================================================
00301 // Std_TestProgress2
00302 //===========================================================================
00303 DEF_STD_CMD_A(CmdTestProgress2);
00304 
00305 CmdTestProgress2::CmdTestProgress2()
00306   : Command("Std_TestProgress2")
00307 {
00308     sGroup          = "Standard-Test";
00309     sMenuText       = "Unbreakable bar";
00310     sToolTipText    = "Test a unbreakable progress bar";
00311     sWhatsThis      = sToolTipText;
00312     sStatusTip      = sToolTipText;
00313     sPixmap         = "Std_Tool7";
00314 }
00315 
00316 void CmdTestProgress2::activated(int iMsg)
00317 {
00318     try
00319     {
00320         QMutex mutex;
00321         mutex.lock();
00322         unsigned long steps = 1000;
00323         Base::SequencerLauncher seq("Starting progress bar", steps);
00324 
00325         for (unsigned long i=0; i<steps;i++)
00326         {
00327             seq.next(false);
00328             QWaitCondition().wait(&mutex, 10);
00329         }
00330     }
00331     catch (...)
00332     {
00333     }
00334 }
00335 
00336 bool CmdTestProgress2::isActive(void)
00337 {
00338     return ( !Base::Sequencer().isRunning() );
00339 }
00340 
00341 //===========================================================================
00342 // Std_TestProgress3
00343 //===========================================================================
00344 DEF_STD_CMD_A(CmdTestProgress3);
00345 
00346 CmdTestProgress3::CmdTestProgress3()
00347   : Command("Std_TestProgress3")
00348 {
00349     sGroup          = "Standard-Test";
00350     sMenuText       = "Nested progress bar";
00351     sToolTipText    = "Test nested progress bar";
00352     sWhatsThis      = sToolTipText;
00353     sStatusTip      = sToolTipText;
00354     sPixmap         = "Std_Tool8";
00355 }
00356 
00357 void CmdTestProgress3::activated(int iMsg)
00358 {
00359     try
00360     {
00361         // level 1
00362         QMutex mutex;
00363         mutex.lock();
00364         unsigned long steps = 5;
00365         Base::SequencerLauncher seq1("Starting progress bar", steps);
00366         for (unsigned long i=0; i<steps;i++)
00367         {
00368             QWaitCondition().wait(&mutex, 200);
00369             seq1.next(true);
00370 
00371             // level 2
00372             unsigned long steps = 6;
00373             Base::SequencerLauncher seq2("Starting progress bar", steps);
00374             for (unsigned long j=0; j<steps;j++)
00375             {
00376                 QWaitCondition().wait(&mutex, 150);
00377                 seq2.next(true);
00378 
00379                 // level 3
00380                 unsigned long steps = 7;
00381                 Base::SequencerLauncher seq3("Starting progress bar", steps);
00382                 for (unsigned long k=0; k<steps;k++)
00383                 {
00384                     QWaitCondition().wait(&mutex, 100);
00385                     seq3.next(true);
00386 
00387                     // level 4
00388                     unsigned long steps = 8;
00389                     Base::SequencerLauncher seq4("Starting progress bar", steps);
00390                     for (unsigned long l=0; l<steps;l++)
00391                     {
00392                         QWaitCondition().wait(&mutex, 5);
00393                         seq4.next(true);
00394                     }
00395                 }
00396             }
00397         }
00398     }
00399     catch (...)
00400     {
00401     }
00402 }
00403 
00404 bool CmdTestProgress3::isActive(void)
00405 {
00406     return ( !Base::Sequencer().isRunning() );
00407 }
00408 
00409 //===========================================================================
00410 // Std_TestProgress4
00411 //===========================================================================
00412 DEF_STD_CMD_A(CmdTestProgress4);
00413 
00414 CmdTestProgress4::CmdTestProgress4()
00415   : Command("Std_TestProgress4")
00416 {
00417     sGroup          = "Standard-Test";
00418     sMenuText       = "Mixed nested bar";
00419     sToolTipText    = "Test a mixed up nested progress bar";
00420     sWhatsThis      = sToolTipText;
00421     sStatusTip      = sToolTipText;
00422     sPixmap         = "Std_Tool7";
00423 }
00424 
00425 void CmdTestProgress4::activated(int iMsg)
00426 {
00427     try
00428     {
00429         QMutex mutex;
00430         mutex.lock();
00431         unsigned long steps = 50;
00432         Base::SequencerLauncher* seq = new Base::SequencerLauncher("Starting progress bar", steps);
00433 
00434         for (unsigned long i=0; i<steps;i++)
00435         {
00436             QWaitCondition().wait(&mutex, 5);
00437             if (i == 45) {
00438                 delete seq;
00439                 seq = 0;
00440             }
00441             if (seq) {
00442                 seq->next(false);
00443             }
00444             Base::SequencerLauncher seq2("Starting second progress bar", steps);
00445             for (unsigned long j=0; j<steps;j++)
00446             {
00447                 QWaitCondition().wait(&mutex, (seq ? 5 : 50));
00448                 seq2.next(true);
00449             }
00450         }
00451     }
00452     catch (...)
00453     {
00454     }
00455 }
00456 
00457 bool CmdTestProgress4::isActive(void)
00458 {
00459     return (!Base::Sequencer().isRunning());
00460 }
00461 
00462 //===========================================================================
00463 // Std_TestProgress5
00464 //===========================================================================
00465 DEF_STD_CMD_A(CmdTestProgress5);
00466 
00467 CmdTestProgress5::CmdTestProgress5()
00468   : Command("Std_TestProgress5")
00469 {
00470     sGroup          = "Standard-Test";
00471     sMenuText       = "From thread";
00472     sToolTipText    = "Test a progress bar from a thread";
00473     sWhatsThis      = sToolTipText;
00474     sStatusTip      = sToolTipText;
00475     sPixmap         = "Std_Tool7";
00476 }
00477 
00478 class BarThread : public QThread
00479 {
00480 public:
00481     BarThread(unsigned long s) : steps(s)
00482     {
00483     }
00484     ~BarThread()
00485     {
00486     }
00487     void run()
00488     {
00489         try
00490         {
00491             QMutex mutex;
00492             mutex.lock();
00493             Base::SequencerLauncher seq("Starting progress bar in thread", steps);
00494 
00495             for (unsigned long i=0; i<this->steps;i++)
00496             {
00497                 seq.next(true);
00498                 QWaitCondition().wait(&mutex, 5);
00499             }
00500             mutex.unlock();
00501         }
00502         catch (...)
00503         {
00504         }
00505 
00506         this->deleteLater();
00507         Base::Console().Message("Thread with %d steps finished\n",this->steps);
00508     }
00509 
00510 private:
00511     unsigned long steps;
00512 };
00513 
00514 void CmdTestProgress5::activated(int iMsg)
00515 {
00516     QEventLoop loop;
00517 
00518     BarThread* thr1 = new BarThread(2000);
00519     QObject::connect(thr1, SIGNAL(finished()), &loop, SLOT(quit()));
00520     thr1->start();
00521     loop.exec();
00522 
00523     BarThread* thr2 = new BarThread(1500);
00524 
00525     QTimer timer;
00526     timer.setSingleShot(true);
00527     QObject::connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
00528     thr2->start();
00529     timer.start(2000); // 2s timeout
00530     loop.exec();
00531 
00532     BarThread* thr3 = new BarThread(1000);
00533     thr3->start();
00534 }
00535 
00536 bool CmdTestProgress5::isActive(void)
00537 {
00538     return (!Base::Sequencer().isRunning());
00539 }
00540 
00541 //===========================================================================
00542 // Std_MDITest
00543 //===========================================================================
00544 DEF_STD_CMD_A(CmdTestMDI1);
00545 
00546 CmdTestMDI1::CmdTestMDI1()
00547   : Command("Std_MDITest1")
00548 {
00549     sGroup          = "Standard-Test";
00550     sMenuText       = "Remove MDI 1";
00551     sToolTipText    = "Remove MDI from main window";
00552     sWhatsThis      = sToolTipText;
00553     sStatusTip      = sToolTipText;
00554 }
00555 
00556 void CmdTestMDI1::activated(int iMsg)
00557 {
00558     MDIView* mdi = getMainWindow()->activeWindow();
00559     getMainWindow()->removeWindow(mdi);
00560 }
00561 
00562 bool CmdTestMDI1::isActive(void)
00563 {
00564     return getMainWindow()->activeWindow();
00565 }
00566 
00567 DEF_STD_CMD_A(CmdTestMDI2);
00568 
00569 CmdTestMDI2::CmdTestMDI2()
00570   : Command("Std_MDITest2")
00571 {
00572     sGroup          = "Standard-Test";
00573     sMenuText       = "Remove MDI 2";
00574     sToolTipText    = "Remove view from MDI area";
00575     sWhatsThis      = sToolTipText;
00576     sStatusTip      = sToolTipText;
00577 }
00578 
00579 void CmdTestMDI2::activated(int iMsg)
00580 {
00581     QMdiArea* area = getMainWindow()->findChild<QMdiArea*>();
00582     if (area) {
00583         MDIView* mdi = getMainWindow()->activeWindow();
00584         area->removeSubWindow(mdi->parentWidget());
00585         mdi->parentWidget()->showNormal();
00586     }
00587 }
00588 
00589 bool CmdTestMDI2::isActive(void)
00590 {
00591     return getMainWindow()->activeWindow();
00592 }
00593 
00594 DEF_STD_CMD_A(CmdTestMDI3);
00595 
00596 CmdTestMDI3::CmdTestMDI3()
00597   : Command("Std_MDITest3")
00598 {
00599     sGroup          = "Standard-Test";
00600     sMenuText       = "Remove MDI 3";
00601     sToolTipText    = "Unset parent and remove from main window";
00602     sWhatsThis      = sToolTipText;
00603     sStatusTip      = sToolTipText;
00604 }
00605 
00606 void CmdTestMDI3::activated(int iMsg)
00607 {
00608     MDIView* mdi = getMainWindow()->activeWindow();
00609     getMainWindow()->removeWindow(mdi);
00610     mdi->setParent(0, Qt::Window | Qt::WindowTitleHint |
00611                    Qt::WindowSystemMenuHint | 
00612                    Qt::WindowMinMaxButtonsHint);
00613     mdi->show();
00614 }
00615 
00616 bool CmdTestMDI3::isActive(void)
00617 {
00618     return getMainWindow()->activeWindow();
00619 }
00620 
00621 
00622 namespace Gui {
00623 
00624 void CreateTestCommands(void)
00625 {
00626     CommandManager &rcCmdMgr = Application::Instance->commandManager();
00627 
00628     rcCmdMgr.addCommand(new Std_TestQM());
00629     rcCmdMgr.addCommand(new FCCmdTest1());
00630     rcCmdMgr.addCommand(new FCCmdTest2());
00631     rcCmdMgr.addCommand(new FCCmdTest3());
00632     rcCmdMgr.addCommand(new FCCmdTest4());
00633     rcCmdMgr.addCommand(new FCCmdTest5());
00634     rcCmdMgr.addCommand(new FCCmdTest6());
00635     rcCmdMgr.addCommand(new CmdTestProgress1());
00636     rcCmdMgr.addCommand(new CmdTestProgress2());
00637     rcCmdMgr.addCommand(new CmdTestProgress3());
00638     rcCmdMgr.addCommand(new CmdTestProgress4());
00639     rcCmdMgr.addCommand(new CmdTestProgress5());
00640     rcCmdMgr.addCommand(new CmdTestMDI1());
00641     rcCmdMgr.addCommand(new CmdTestMDI2());
00642     rcCmdMgr.addCommand(new CmdTestMDI3());
00643 }
00644 
00645 } // namespace Gui

Generated on Wed Nov 23 19:00:03 2011 for FreeCAD by  doxygen 1.6.1