tomain.cpp

Go to the documentation of this file.
00001 //***************************************************************************
00002 /*
00003  * TOra - An Oracle Toolkit for DBA's and developers
00004  * Copyright (C) 2000-2001,2001 Underscore AB
00005  * 
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation;  only version 2 of
00009  * the License is valid for this program.
00010  * 
00011  * This program 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 General Public License for more details.
00015  * 
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software
00018  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00019  *
00020  *      As a special exception, you have permission to link this program
00021  *      with the Oracle Client libraries and distribute executables, as long
00022  *      as you follow the requirements of the GNU GPL in regard to all of the
00023  *      software in the executable aside from Oracle client libraries.
00024  *
00025  *      Specifically you are not permitted to link this program with the
00026  *      Qt/UNIX, Qt/Windows or Qt Non Commercial products of TrollTech.
00027  *      And you are not permitted to distribute binaries compiled against
00028  *      these libraries without written consent from Underscore AB. Observe
00029  *      that this does not disallow linking to the Qt Free Edition.
00030  *
00031  * All trademarks belong to their respective owners.
00032  *
00033  ****************************************************************************/
00034 
00035 #ifdef TO_KDE
00036 #include <kmenubar.h>
00037 #include <kfiledialog.h>
00038 #include <kstatusbar.h>
00039 #endif
00040 
00041 #include <qapplication.h>
00042 #include <qcheckbox.h>
00043 #include <qclipboard.h>
00044 #include <qcombobox.h>
00045 #include <qevent.h>
00046 #include <qfiledialog.h>
00047 #include <qfile.h>
00048 #include <qlabel.h>
00049 #include <qmenubar.h>
00050 #include <qmessagebox.h>
00051 #include <qnamespace.h>
00052 #include <qpopupmenu.h>
00053 #include <qpushbutton.h>
00054 #include <qstatusbar.h>
00055 #include <qtoolbar.h>
00056 #include <qtoolbutton.h>
00057 #include <qworkspace.h>
00058 #include <qfileinfo.h>
00059 #include <qvbox.h>
00060 #include <qregexp.h>
00061 
00062 #include "toconf.h"
00063 #include "tomain.h"
00064 #include "totool.h"
00065 #include "tonewconnection.h"
00066 #include "toabout.h"
00067 #include "topreferences.h"
00068 #include "tosearchreplace.h"
00069 #include "tohelp.h"
00070 #include "tomemoeditor.h"
00071 #include "toeditwidget.h"
00072 #include "toconnection.h"
00073 #include "tomessageui.h"
00074 
00075 #include "tomain.moc"
00076 #include "tomessageui.moc"
00077 #ifdef TO_KDE
00078 #include "tomainwindow.kde.moc"
00079 #else
00080 #include "tomainwindow.moc"
00081 #endif
00082 
00083 #include "icons/connect.xpm"
00084 #include "icons/disconnect.xpm"
00085 #include "icons/fileopen.xpm"
00086 #include "icons/filesave.xpm"
00087 #include "icons/print.xpm"
00088 #include "icons/commit.xpm"
00089 #include "icons/rollback.xpm"
00090 #include "icons/undo.xpm"
00091 #include "icons/redo.xpm"
00092 #include "icons/cut.xpm"
00093 #include "icons/copy.xpm"
00094 #include "icons/paste.xpm"
00095 #include "icons/toramini.xpm"
00096 #include "icons/up.xpm"
00097 #include "icons/trash.xpm"
00098 
00099 #define DEFAULT_TITLE "TOra %s"
00100 
00101 const int toMain::TO_FILE_MENU          = 10;
00102 const int toMain::TO_EDIT_MENU          = 20;
00103 const int toMain::TO_TOOLS_MENU         = 30;
00104 const int toMain::TO_WINDOWS_MENU       = 40;
00105 const int toMain::TO_HELP_MENU          = 50;
00106 
00107 const int toMain::TO_TOOL_MENU_ID       = 2000;
00108 const int toMain::TO_TOOL_MENU_ID_END   = 2999;
00109 
00110 const int toMain::TO_TOOL_ABOUT_ID      = 3000;
00111 const int toMain::TO_TOOL_ABOUT_ID_END  = 3999;
00112 
00113 #define TO_STATUS_ID            4000
00114 #define TO_STATUS_ID_END        4999
00115 #define TO_NEW_CONNECTION       100
00116 #define TO_CLOSE_CONNECTION     101
00117 #define TO_FILE_OPEN            102
00118 #define TO_FILE_SAVE            103
00119 #define TO_FILE_SAVE_AS         104
00120 #define TO_FILE_COMMIT          105
00121 #define TO_FILE_ROLLBACK        106
00122 #define TO_FILE_CURRENT         107
00123 #define TO_FILE_CLEARCACHE      108
00124 #define TO_FILE_PRINT           109
00125 #define TO_FILE_QUIT            110
00126 
00127 #define TO_EDIT_UNDO            200
00128 #define TO_EDIT_REDO            201
00129 #define TO_EDIT_CUT             202
00130 #define TO_EDIT_COPY            203
00131 #define TO_EDIT_PASTE           204
00132 #define TO_EDIT_SELECT_ALL      205
00133 #define TO_EDIT_OPTIONS         206
00134 #define TO_EDIT_READ_ALL        207
00135 #define TO_EDIT_SEARCH          208
00136 #define TO_EDIT_SEARCH_NEXT     209
00137 
00138 #define TO_WINDOWS_TILE         300
00139 #define TO_WINDOWS_CASCADE      301
00140 #define TO_WINDOWS_CLOSE        302
00141 #define TO_WINDOWS_CLOSE_ALL    303
00142 #define TO_WINDOWS_WINDOWS      310
00143 #define TO_WINDOWS_END          399
00144 
00145 #define TO_HELP_CONTENTS        900
00146 #define TO_HELP_CONTEXT         901
00147 #define TO_HELP_ABOUT           902
00148 #define TO_HELP_LICENSE         903
00149 #define TO_HELP_QUOTES          904
00150 #define TO_HELP_REGISTER        905
00151 
00152 #define TO_TOOLS                1000
00153 #define TO_ABOUT_ID_OFFSET      (toMain::TO_TOOL_ABOUT_ID-TO_TOOLS)
00154 
00155 QString toCheckLicense(bool);
00156 bool toFreeware(void);
00157 
00158 toMain::toMain()
00159   : toMainWindow()
00160 {
00161   qApp->setMainWidget(this);
00162   setDockMenuEnabled(true);
00163 
00164   Edit=NULL;
00165 
00166   FileMenu=new QPopupMenu(this);
00167   FileMenu->insertItem(QPixmap((const char **)connect_xpm),
00168                        "&New Connection...",TO_NEW_CONNECTION);
00169   FileMenu->insertItem(QPixmap((const char **)disconnect_xpm),
00170                        "&Close Connection",this,SLOT(delConnection()),0,TO_CLOSE_CONNECTION);
00171   FileMenu->insertSeparator();
00172   FileMenu->insertItem(QPixmap((const char **)commit_xpm),"&Commit Connection",TO_FILE_COMMIT);
00173   FileMenu->insertItem(QPixmap((const char **)rollback_xpm),"&Rollback Connection",TO_FILE_ROLLBACK);
00174   FileMenu->insertItem("C&urrent Connection",TO_FILE_CURRENT);
00175   FileMenu->insertItem(QPixmap((const char **)trash_xpm),"Reread Object Cache",TO_FILE_CLEARCACHE);
00176   FileMenu->insertSeparator();
00177   FileMenu->insertItem(QPixmap((const char **)fileopen_xpm),"&Open File...",TO_FILE_OPEN);
00178   FileMenu->insertItem(QPixmap((const char **)filesave_xpm),"&Save",TO_FILE_SAVE);
00179   FileMenu->insertItem("Save A&s...",TO_FILE_SAVE_AS);
00180   FileMenu->insertSeparator();
00181   FileMenu->insertItem(QPixmap((const char **)print_xpm),"&Print..",TO_FILE_PRINT);
00182   FileMenu->insertSeparator();
00183   FileMenu->insertItem("&Quit",TO_FILE_QUIT);
00184   menuBar()->insertItem("&File",FileMenu,TO_FILE_MENU);
00185   FileMenu->setAccel(Key_G|CTRL,TO_NEW_CONNECTION);
00186   FileMenu->setAccel(Key_O|CTRL,TO_FILE_OPEN);
00187   FileMenu->setAccel(Key_S|CTRL,TO_FILE_SAVE);
00188   FileMenu->setAccel(Key_W|CTRL,TO_FILE_COMMIT);
00189   FileMenu->setAccel(Key_R|CTRL,TO_FILE_ROLLBACK);
00190   FileMenu->setAccel(Key_U|CTRL,TO_FILE_CURRENT);
00191   connect(FileMenu,SIGNAL(aboutToShow()),this,SLOT( editFileMenu()));
00192 
00193   EditMenu=new QPopupMenu(this);
00194   EditMenu->insertItem(QPixmap((const char **)undo_xpm),"&Undo",TO_EDIT_UNDO);
00195   EditMenu->insertItem(QPixmap((const char **)redo_xpm),"&Redo",TO_EDIT_REDO);
00196   EditMenu->insertSeparator();
00197   EditMenu->insertItem(QPixmap((const char **)cut_xpm),"Cu&t",TO_EDIT_CUT);
00198   EditMenu->insertItem(QPixmap((const char **)copy_xpm),"&Copy",TO_EDIT_COPY);
00199   EditMenu->insertItem(QPixmap((const char **)paste_xpm),"&Paste",TO_EDIT_PASTE);
00200   EditMenu->insertSeparator();
00201   EditMenu->insertItem("&Search && Replace",TO_EDIT_SEARCH);
00202   EditMenu->insertItem("Search &Next",TO_EDIT_SEARCH_NEXT);
00203   EditMenu->insertItem("Select &All",TO_EDIT_SELECT_ALL);
00204   EditMenu->insertItem("Read All &Items",TO_EDIT_READ_ALL);
00205   EditMenu->insertSeparator();
00206   EditMenu->insertItem("&Options...",TO_EDIT_OPTIONS);
00207   EditMenu->setAccel(Key_Z|CTRL,TO_EDIT_UNDO);
00208   EditMenu->setAccel(Key_Y|CTRL,TO_EDIT_REDO);
00209   EditMenu->setAccel(Key_X|CTRL,TO_EDIT_CUT);
00210   EditMenu->setAccel(Key_C|CTRL,TO_EDIT_COPY);
00211   EditMenu->setAccel(Key_V|CTRL,TO_EDIT_PASTE);
00212   EditMenu->setAccel(Key_F|CTRL,TO_EDIT_SEARCH);
00213   EditMenu->setAccel(Key_F3,TO_EDIT_SEARCH_NEXT);
00214   connect(EditMenu,SIGNAL(aboutToShow()),this,SLOT( editFileMenu()));
00215   menuBar()->insertItem("&Edit",EditMenu,TO_EDIT_MENU);
00216 
00217   std::map<QString,toTool *> &tools=toTool::tools();
00218 
00219   QToolBar *toolbar=toAllocBar(this,"Application",QString::null);
00220 
00221   LoadButton=new QToolButton(QPixmap((const char **)fileopen_xpm),
00222                              "Load file into editor",
00223                              "Load file into editor",
00224                              this,SLOT(loadButton()),toolbar);
00225   SaveButton=new QToolButton(QPixmap((const char **)filesave_xpm),
00226                              "Save file from editor",
00227                              "Save file from editor",
00228                              this,SLOT(saveButton()),toolbar);
00229   PrintButton=new QToolButton(QPixmap((const char **)print_xpm),
00230                              "Print",
00231                              "Print",
00232                              this,SLOT(printButton()),toolbar);
00233   PrintButton->setEnabled(false);
00234   LoadButton->setEnabled(false);
00235   SaveButton->setEnabled(false);
00236   toolbar->addSeparator();
00237   UndoButton=new QToolButton(QPixmap((const char **)undo_xpm),
00238                              "Undo",
00239                              "Undo",
00240                              this,SLOT(undoButton()),toolbar);
00241   RedoButton=new QToolButton(QPixmap((const char **)redo_xpm),
00242                              "Redo",
00243                              "Redo",
00244                              this,SLOT(redoButton()),toolbar);
00245   CutButton=new QToolButton(QPixmap((const char **)cut_xpm),
00246                              "Cut to clipboard",
00247                              "Cut to clipboard",
00248                              this,SLOT(cutButton()),toolbar);
00249   CopyButton=new QToolButton(QPixmap((const char **)copy_xpm),
00250                              "Copy to clipboard",
00251                              "Copy to clipboard",
00252                              this,SLOT(copyButton()),toolbar);
00253   PasteButton=new QToolButton(QPixmap((const char **)paste_xpm),
00254                               "Paste from clipboard",
00255                               "Paste from clipboard",
00256                               this,SLOT(pasteButton()),toolbar);
00257   UndoButton->setEnabled(false);
00258   RedoButton->setEnabled(false);
00259   CutButton->setEnabled(false);
00260   CopyButton->setEnabled(false);
00261   PasteButton->setEnabled(false);
00262 
00263   ToolsMenu=new QPopupMenu(this);
00264 
00265 #ifdef TOOL_TOOLBAR
00266   toolbar=new toAllocBar(this,"Tools",QString::null);
00267 #else
00268   toolbar->addSeparator();
00269 #endif
00270 
00271   int toolID=TO_TOOLS;
00272   int lastPriorityPix=0;
00273   int lastPriorityMenu=0;
00274   SQLEditor=-1;
00275   DefaultTool=toolID;
00276   QString defName=toTool::globalConfig(CONF_DEFAULT_TOOL,"");
00277 
00278   HelpMenu=new QPopupMenu(this);
00279   HelpMenu->insertItem("C&urrent Context",TO_HELP_CONTEXT);
00280   HelpMenu->insertItem("&Contents",TO_HELP_CONTENTS);
00281   HelpMenu->insertSeparator();
00282   HelpMenu->insertItem("&About TOra",TO_HELP_ABOUT);
00283   HelpMenu->insertItem("&License",TO_HELP_LICENSE);
00284   HelpMenu->insertItem("&Quotes",TO_HELP_QUOTES);
00285   HelpMenu->setAccel(Key_F1,TO_HELP_CONTEXT);
00286   if (!toFreeware()) {
00287     HelpMenu->insertSeparator();
00288     HelpMenu->insertItem("&Register",TO_HELP_REGISTER);
00289   }
00290   QPopupMenu *toolAbout=NULL;
00291 
00292   for (std::map<QString,toTool *>::iterator i=tools.begin();i!=tools.end();i++) {
00293     const QPixmap *pixmap=(*i).second->toolbarImage();
00294     const char *toolTip=(*i).second->toolbarTip();
00295     const char *menuName=(*i).second->menuItem();
00296 
00297     QString tmp=(*i).second->name();
00298     tmp+=CONF_TOOL_ENABLE;
00299     if (toTool::globalConfig(tmp,"Yes").isEmpty())
00300       continue;
00301 
00302     if (defName==menuName)
00303       DefaultTool=toolID;
00304 
00305     int priority=(*i).second->priority();
00306     if (priority/100!=lastPriorityPix/100&&
00307         pixmap) {
00308       toolbar->addSeparator();
00309       lastPriorityPix=priority;
00310     }
00311     if (priority/100!=lastPriorityMenu/100&&
00312         menuName) {
00313       ToolsMenu->insertSeparator();
00314       lastPriorityMenu=priority;
00315     }
00316 
00317     if (pixmap) {
00318       if (!toolTip)
00319         toolTip="";
00320       NeedConnection[new QToolButton(*pixmap,
00321                                      toolTip,
00322                                      toolTip,
00323                                      (*i).second,
00324                                      SLOT(createWindow(void)),
00325                                      toolbar)]=(*i).second;
00326     }
00327 
00328     if (menuName) {
00329       if (pixmap)
00330         ToolsMenu->insertItem(*pixmap,menuName,toolID);
00331       else
00332         ToolsMenu->insertItem(menuName,toolID);
00333       ToolsMenu->setItemEnabled(toolID,false);
00334     }
00335 
00336     if ((*i).second->hasAbout()&&menuName) {
00337       if (!toolAbout) {
00338         toolAbout=new QPopupMenu(this);
00339         HelpMenu->insertItem("Tools",toolAbout);
00340       }
00341       if (pixmap)
00342         toolAbout->insertItem(*pixmap,menuName,toolID+TO_ABOUT_ID_OFFSET);
00343       else
00344         toolAbout->insertItem(menuName,toolID+TO_ABOUT_ID_OFFSET);
00345     }
00346 
00347     Tools[toolID]=(*i).second;
00348 
00349     toolID++;
00350   }
00351 
00352 #ifndef TOOL_TOOLBAR
00353   toolbar->setStretchableWidget(new QLabel("",toolbar));
00354 #endif
00355 
00356   new QToolButton(QPixmap((const char **)connect_xpm),
00357                   "Connect to database",
00358                   "Connect to database",
00359                   this,SLOT(addConnection()),toolbar);
00360   DisconnectButton=new QToolButton(QPixmap((const char **)disconnect_xpm),
00361                                    "Disconnect current connection",
00362                                    "Disconnect current connection",
00363                                    this,SLOT(delConnection()),toolbar);
00364   DisconnectButton->setEnabled(false);
00365   toolbar->addSeparator();
00366   NeedConnection[new QToolButton(QPixmap((const char **)commit_xpm),
00367                                  "Commit connection",
00368                                  "Commit connection",
00369                                  this,SLOT(commitButton()),toolbar)]=NULL;
00370   NeedConnection[new QToolButton(QPixmap((const char **)rollback_xpm),
00371                                  "Rollback connection",
00372                                  "Rollback connection",
00373                                  this,SLOT(rollbackButton()),toolbar)]=NULL;
00374   toolbar->addSeparator();
00375   ConnectionSelection=new QComboBox(toolbar);
00376   ConnectionSelection->setFixedWidth(200);
00377   ConnectionSelection->setFocusPolicy(NoFocus);
00378   connect(ConnectionSelection,SIGNAL(activated(int)),this,SLOT(changeConnection()));
00379 
00380   menuBar()->insertItem("&Tools",ToolsMenu,TO_TOOLS_MENU);
00381 
00382   WindowsMenu=new QPopupMenu(this);
00383   WindowsMenu->setCheckable(true);
00384   connect(WindowsMenu,SIGNAL(aboutToShow()),this,SLOT( windowsMenu()));
00385   menuBar()->insertItem("&Window",WindowsMenu,TO_WINDOWS_MENU);
00386 
00387   menuBar()->insertSeparator();
00388 
00389   menuBar()->insertItem("&Help",HelpMenu,TO_HELP_MENU);
00390 
00391   char buffer[100];
00392   sprintf(buffer,DEFAULT_TITLE,TOVERSION);
00393   setCaption(buffer);
00394 
00395 #ifdef TO_KDE
00396   KDockWidget *mainDock=createDockWidget(buffer,QPixmap((const char **)toramini_xpm));
00397   Workspace=new QWorkspace(mainDock);
00398   mainDock->setWidget(Workspace);
00399   setView(mainDock);
00400   setMainDockWidget(mainDock);
00401   mainDock->setEnableDocking(KDockWidget::DockNone);
00402 #else
00403   Workspace=new QWorkspace(this);
00404   setCentralWidget(Workspace);
00405 #endif
00406   setIcon(QPixmap((const char **)toramini_xpm));
00407 
00408   statusBar()->message("Ready");
00409   menuBar()->setItemEnabled(TO_CLOSE_CONNECTION,false);
00410   menuBar()->setItemEnabled(TO_FILE_COMMIT,false);
00411   menuBar()->setItemEnabled(TO_FILE_ROLLBACK,false);
00412   menuBar()->setItemEnabled(TO_FILE_CLEARCACHE,false);
00413   DisconnectButton->setEnabled(false);
00414 
00415   for (std::map<QToolButton *,toTool *>::iterator j=NeedConnection.begin();
00416        j!=NeedConnection.end();j++)
00417     (*j).first->setEnabled(false);
00418 
00419   connect(menuBar(),SIGNAL(activated(int)),this,SLOT(commandCallback(int)));
00420 
00421   RowLabel=new QLabel(statusBar());
00422   statusBar()->addWidget(RowLabel,0,true);
00423   RowLabel->setMinimumWidth(60);
00424   RowLabel->hide();
00425 
00426   ColumnLabel=new QLabel(statusBar());
00427   statusBar()->addWidget(ColumnLabel,0,true);
00428   ColumnLabel->setMinimumWidth(60);
00429   ColumnLabel->hide();
00430 
00431   QToolButton *dispStatus=new toPopupButton(statusBar());
00432   dispStatus->setIconSet(QPixmap((const char **)up_xpm));
00433   statusBar()->addWidget(dispStatus,0,true);
00434   StatusMenu=new QPopupMenu(dispStatus);
00435   dispStatus->setPopup(StatusMenu);
00436   connect(StatusMenu,SIGNAL(aboutToShow()),
00437           this,SLOT(statusMenu()));
00438   connect(StatusMenu,SIGNAL(activated(int)),this,SLOT(commandCallback(int)));
00439 
00440   toolID=TO_TOOLS;
00441   for (std::map<QString,toTool *>::iterator k=tools.begin();k!=tools.end();k++) {
00442     (*k).second->customSetup(toolID);
00443     toolID++;
00444   }
00445   Search=NULL;
00446 
00447   if (!toTool::globalConfig(CONF_MAXIMIZE_MAIN,"Yes").isEmpty())
00448     showMaximized();
00449   show();
00450 
00451   QString welcome;
00452 
00453   do {
00454     welcome=toCheckLicense(false);
00455   } while(welcome.isNull());
00456 
00457   toStatusMessage(welcome,true);
00458 
00459   connect(&Poll,SIGNAL(timeout()),this,SLOT(checkCaching()));
00460 
00461   try {
00462     toNewConnection newConnection(this,"First connection",true);
00463     
00464     toConnection *conn;
00465     
00466     do {
00467       conn=NULL;
00468       if (newConnection.exec()) {
00469         conn=newConnection.makeConnection();
00470       } else {
00471         break;
00472       }
00473     } while(!conn);
00474     
00475     if (conn)
00476       addConnection(conn);
00477   } TOCATCH
00478   connect(toMainWidget()->workspace(),SIGNAL(windowActivated(QWidget *)),
00479           this,SLOT(windowActivated(QWidget *)));
00480 }
00481 
00482 void toMain::windowActivated(QWidget *widget)
00483 {
00484   if (toTool::globalConfig(CONF_CHANGE_CONNECTION,"Yes").isEmpty())
00485     return;
00486   toToolWidget *tool=dynamic_cast<toToolWidget *>(widget);
00487   if (tool) {
00488     toConnection &conn=tool->connection();
00489     int pos=0;
00490     for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++) {
00491       if (&conn==*i) {
00492         ConnectionSelection->setCurrentItem(pos);
00493         changeConnection();
00494         break;
00495       }
00496       pos++;
00497     }
00498   }
00499 }
00500 
00501 void toMain::editFileMenu(void) // Ugly hack to disable edit with closed child windows
00502 {
00503   menuBar()->setItemEnabled(TO_EDIT_UNDO,Edit&&Edit->undoEnabled());
00504   menuBar()->setItemEnabled(TO_EDIT_REDO,Edit&&Edit->redoEnabled());
00505   menuBar()->setItemEnabled(TO_EDIT_CUT,Edit&&Edit->cutEnabled());
00506   menuBar()->setItemEnabled(TO_EDIT_COPY,Edit&&Edit->copyEnabled());
00507   menuBar()->setItemEnabled(TO_EDIT_PASTE,Edit&&Edit->pasteEnabled());
00508   menuBar()->setItemEnabled(TO_EDIT_SELECT_ALL,Edit&&Edit->selectAllEnabled());
00509   menuBar()->setItemEnabled(TO_EDIT_READ_ALL,Edit&&Edit->readAllEnabled());
00510   menuBar()->setItemEnabled(TO_EDIT_SEARCH,Edit&&Edit->searchEnabled());
00511   menuBar()->setItemEnabled(TO_FILE_OPEN,Edit&&Edit->openEnabled());
00512   menuBar()->setItemEnabled(TO_FILE_SAVE,Edit&&Edit->saveEnabled());
00513   menuBar()->setItemEnabled(TO_FILE_SAVE_AS,Edit&&Edit->saveEnabled());
00514   menuBar()->setItemEnabled(TO_EDIT_SEARCH_NEXT,
00515                             Search&&Search->searchNextAvailable());
00516 }
00517 
00518 void toMain::windowsMenu(void)
00519 {
00520   WindowsMenu->clear();
00521 
00522   WindowsMenu->insertItem("C&lose",TO_WINDOWS_CLOSE);
00523   if (!workspace()->activeWindow())
00524     WindowsMenu->setItemEnabled(TO_WINDOWS_CLOSE,false);
00525 
00526   WindowsMenu->insertItem("Close &All",TO_WINDOWS_CLOSE_ALL);
00527   WindowsMenu->insertSeparator();
00528   WindowsMenu->insertItem("&Cascade",TO_WINDOWS_CASCADE);
00529   WindowsMenu->insertItem("&Tile",TO_WINDOWS_TILE);
00530   if (workspace()->windowList().isEmpty()) {
00531     WindowsMenu->setItemEnabled(TO_WINDOWS_CASCADE,false);
00532     WindowsMenu->setItemEnabled(TO_WINDOWS_TILE,false);
00533     WindowsMenu->setItemEnabled(TO_WINDOWS_CLOSE_ALL,false);
00534   } else {
00535     WindowsMenu->insertSeparator();
00536     QRegExp strip(" <[0-9]+>$");
00537     for (unsigned int i=0;i<workspace()->windowList().count();i++) {
00538       QWidget *widget=workspace()->windowList().at(i);
00539       QString caption=widget->caption();
00540       caption.replace(strip,"");
00541       WindowsMenu->insertItem(caption,TO_WINDOWS_WINDOWS+i);
00542       WindowsMenu->setItemChecked(TO_WINDOWS_WINDOWS+i,workspace()->activeWindow()==workspace()->windowList().at(i));
00543       if (i<9) {
00544         WindowsMenu->setAccel(Key_1+i|CTRL,TO_WINDOWS_WINDOWS+i);
00545         caption+=" <";
00546         caption+=QString::number(i+1);
00547         caption+=">";
00548       }
00549       widget->setCaption(caption);
00550     }
00551   }
00552 }
00553 
00554 void toMain::commandCallback(int cmd)
00555 {
00556   QWidget *focus=qApp->focusWidget();
00557   
00558   if (focus) {
00559     toEditWidget *edit=findEdit(focus);
00560     if (edit&&edit!=Edit)
00561       setEditWidget(edit);
00562     else if (focus->inherits("QLineEdit")||
00563              focus->isA("QSpinBox"))
00564       editDisable(Edit);
00565   }
00566 
00567   if (Tools[cmd])
00568     Tools[cmd]->createWindow();
00569   else if (cmd>=TO_TOOL_ABOUT_ID&&cmd<=TO_TOOL_ABOUT_ID_END) {
00570     if (Tools[cmd-TO_ABOUT_ID_OFFSET])
00571       Tools[cmd-TO_ABOUT_ID_OFFSET]->about(this);
00572   } else if (cmd>=TO_WINDOWS_WINDOWS&&cmd<=TO_WINDOWS_END) {
00573     if (cmd-TO_WINDOWS_WINDOWS<int(workspace()->windowList().count()))
00574       workspace()->windowList().at(cmd-TO_WINDOWS_WINDOWS)->setFocus();
00575   } else if (cmd>=TO_STATUS_ID&&cmd<=TO_STATUS_ID_END) {
00576     QString str=StatusMenu->text(cmd);
00577     new toMemoEditor(this,str);
00578   } else {
00579     QWidget *currWidget=qApp->focusWidget();
00580     toEditWidget *edit=NULL;
00581     while(currWidget&&!edit) {
00582       edit=dynamic_cast<toEditWidget *>(currWidget);
00583       currWidget=currWidget->parentWidget();
00584     }
00585     if (edit) {
00586       switch(cmd) {
00587       case TO_EDIT_REDO:
00588         edit->editRedo();
00589         break;
00590       case TO_EDIT_UNDO:
00591         edit->editUndo();
00592         break;
00593       case TO_EDIT_COPY:
00594         edit->editCopy();
00595         break;
00596       case TO_EDIT_PASTE:
00597         edit->editPaste();
00598         break;
00599       case TO_EDIT_CUT:
00600         edit->editCut();
00601         break;
00602       case TO_EDIT_SELECT_ALL:
00603         edit->editSelectAll();;
00604         break;
00605       case TO_EDIT_READ_ALL:
00606         edit->editReadAll();
00607         break;
00608       case TO_EDIT_SEARCH:
00609         if (!Search)
00610           Search=new toSearchReplace(this);
00611         edit->editSearch(Search);
00612         break;
00613       case TO_FILE_OPEN:
00614         edit->editOpen();
00615         break;
00616       case TO_FILE_SAVE_AS:
00617         edit->editSave(true);
00618         break;
00619       case TO_FILE_SAVE:
00620         edit->editSave(false);
00621         break;
00622       case TO_FILE_PRINT:
00623         edit->editPrint();
00624         break;
00625       }
00626     }
00627     switch(cmd) {
00628     case TO_FILE_COMMIT:
00629       try {
00630         toConnection &conn=currentConnection();
00631         emit willCommit(conn,true);
00632         conn.commit();
00633         setNeedCommit(conn,false);
00634       } TOCATCH
00635       break;
00636     case TO_FILE_CLEARCACHE:
00637       try {
00638         currentConnection().rereadCache();
00639       } TOCATCH
00640       toMainWidget()->checkCaching();
00641       break;
00642     case TO_FILE_ROLLBACK:
00643       try {
00644         toConnection &conn=currentConnection();
00645         emit willCommit(conn,false);
00646         conn.rollback();
00647         setNeedCommit(conn,false);
00648       } TOCATCH
00649       break;
00650     case TO_FILE_CURRENT:
00651       ConnectionSelection->setFocus();
00652       break;
00653     case TO_FILE_QUIT:
00654       close(true);
00655       break;
00656     case TO_EDIT_SEARCH_NEXT:
00657       if (Search)
00658         Search->searchNext();
00659       break;
00660     case TO_WINDOWS_CASCADE:
00661       workspace()->cascade();
00662       break;
00663     case TO_WINDOWS_TILE:
00664       workspace()->tile();
00665       break;
00666     case TO_NEW_CONNECTION:
00667       addConnection();
00668       break;
00669     case TO_HELP_CONTEXT:
00670       contextHelp();
00671       break;
00672     case TO_HELP_CONTENTS:
00673       toHelp::displayHelp("toc.htm");
00674       break;
00675     case TO_HELP_ABOUT:
00676     case TO_HELP_LICENSE:
00677     case TO_HELP_QUOTES:
00678       {
00679         toAbout *about=new toAbout(cmd-TO_HELP_ABOUT,this,"About TOra",true);
00680         about->exec();
00681         delete about;
00682       }
00683       break;
00684     case TO_HELP_REGISTER:
00685       toCheckLicense(true);
00686       break;
00687     case TO_EDIT_OPTIONS:
00688       toPreferences::displayPreferences(this);
00689       break;
00690     case TO_WINDOWS_CLOSE_ALL:
00691       while (workspace()->windowList().count()>0&&workspace()->windowList().at(0))
00692         if (workspace()->windowList().at(0)&&
00693             !workspace()->windowList().at(0)->close(true))
00694           return;
00695       break;
00696     case TO_WINDOWS_CLOSE:
00697       QWidget *widget=workspace()->activeWindow();
00698       if (widget)
00699         widget->close(true);
00700       break;
00701     }
00702   }
00703 }
00704 
00705 void toMain::addConnection(void)
00706 {
00707   try {
00708     toNewConnection newConnection(this,"New connection",true);
00709 
00710     toConnection *conn=NULL;
00711 
00712     if (newConnection.exec())
00713       conn=newConnection.makeConnection();
00714 
00715     if (conn)
00716       addConnection(conn);
00717   } TOCATCH
00718 }
00719 
00720 toConnection &toMain::currentConnection()
00721 {
00722   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++) {
00723     if (ConnectionSelection->currentText().startsWith((*i)->description())) {
00724       return *(*i);
00725     }
00726   }
00727   throw QString("Can't find active connection");
00728 }
00729 
00730 void toMain::addConnection(toConnection *conn)
00731 {
00732   int j=0;
00733   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++,j++) {
00734     if ((*i)->description()==conn->description()) {
00735       ConnectionSelection->setCurrentItem(j);
00736       createDefault();
00737       return;
00738     }
00739   }
00740 
00741   Connections.insert(Connections.end(),conn);
00742   ConnectionSelection->insertItem(conn->description());
00743   ConnectionSelection->setCurrentItem(ConnectionSelection->count()-1);
00744 
00745   if (ConnectionSelection->count()==1) {
00746     menuBar()->setItemEnabled(TO_FILE_COMMIT,true);
00747     menuBar()->setItemEnabled(TO_FILE_ROLLBACK,true);
00748     menuBar()->setItemEnabled(TO_FILE_CLEARCACHE,true);
00749     menuBar()->setItemEnabled(TO_CLOSE_CONNECTION,true);
00750     DisconnectButton->setEnabled(true);
00751   }
00752 
00753   checkCaching();
00754 
00755   changeConnection();
00756   emit addedConnection(conn->description());
00757   createDefault();
00758 }
00759 
00760 void toMain::setNeedCommit(toConnection &conn,bool needCommit)
00761 {
00762   int pos=0;
00763   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++) {
00764     if (conn.description()==(*i)->description()) {
00765       QString dsc=conn.description();
00766       if (needCommit)
00767         dsc+=" *";
00768       ConnectionSelection->changeItem(dsc,pos);
00769       break;
00770     }
00771     pos++;
00772   }
00773   conn.setNeedCommit(needCommit);
00774 }
00775 
00776 bool toMain::delConnection(void)
00777 {
00778   toConnection *conn=NULL;
00779   int pos=0;
00780   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++) {
00781     if (ConnectionSelection->currentText().startsWith((*i)->description())) {
00782       conn=(*i);
00783       if (conn->needCommit()) {
00784         QString str("Commit work in session to ");
00785         str.append(conn->description());
00786         str.append(" before closing it?");
00787         switch(TOMessageBox::warning(this,"Commit work?",str,"&Yes","&No","&Cancel")) {
00788         case 0:
00789           conn->commit();
00790           break;
00791         case 1:
00792           conn->rollback();
00793           break;
00794         case 2:
00795           return false;
00796         }
00797       }
00798       if (!conn->closeWidgets())
00799         return false;
00800       emit removedConnection(conn->description());
00801       Connections.erase(i);
00802       ConnectionSelection->removeItem(pos);
00803       if (ConnectionSelection->count())
00804         ConnectionSelection->setCurrentItem(max(pos-1,0));
00805       delete conn;
00806       break;
00807     }
00808     pos++;
00809   }
00810   if (ConnectionSelection->count()==0) {
00811     menuBar()->setItemEnabled(TO_FILE_COMMIT,false);
00812     menuBar()->setItemEnabled(TO_FILE_ROLLBACK,false);
00813     menuBar()->setItemEnabled(TO_FILE_CLEARCACHE,false);
00814     menuBar()->setItemEnabled(TO_CLOSE_CONNECTION,false);
00815     DisconnectButton->setEnabled(false);
00816     for (std::map<QToolButton *,toTool *>::iterator i=NeedConnection.begin();
00817          i!=NeedConnection.end();i++)
00818       (*i).first->setEnabled(false);
00819     for (std::map<int,toTool *>::iterator j=Tools.begin();j!=Tools.end();j++)
00820       menuBar()->setItemEnabled((*j).first,false);
00821   } else
00822     changeConnection();
00823   return true;
00824 }
00825 
00826 std::list<QString> toMain::connections(void)
00827 {
00828   std::list<QString> ret;
00829   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++)
00830     toPush(ret,(*i)->description());
00831   return ret;
00832 }
00833 
00834 toConnection &toMain::connection(const QString &str)
00835 {
00836   for (std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++)
00837     if ((*i)->description()==str)
00838       return *(*i);
00839   throw QString("Couldn't find specified connectionts (%1)").arg(str);
00840 }
00841 
00842 void toMain::loadButton(void)
00843 {
00844   commandCallback(TO_FILE_OPEN);
00845 }
00846 
00847 void toMain::commitButton(void)
00848 {
00849   commandCallback(TO_FILE_COMMIT);
00850 }
00851 
00852 void toMain::rollbackButton(void)
00853 {
00854   commandCallback(TO_FILE_ROLLBACK);
00855 }
00856 
00857 void toMain::saveButton(void)
00858 {
00859   commandCallback(TO_FILE_SAVE);
00860 }
00861 
00862 void toMain::undoButton(void)
00863 {
00864   commandCallback(TO_EDIT_UNDO);
00865 }
00866 
00867 void toMain::redoButton(void)
00868 {
00869   commandCallback(TO_EDIT_REDO);
00870 }
00871 
00872 void toMain::copyButton(void)
00873 {
00874   commandCallback(TO_EDIT_COPY);
00875 }
00876 
00877 void toMain::cutButton(void)
00878 {
00879   commandCallback(TO_EDIT_CUT);
00880 }
00881 
00882 void toMain::pasteButton(void)
00883 {
00884   commandCallback(TO_EDIT_PASTE);
00885 }
00886 
00887 void toMain::printButton(void)
00888 {
00889   commandCallback(TO_FILE_PRINT);
00890 }
00891 
00892 void toMain::setEditWidget(toEditWidget *edit)
00893 {
00894   toMain *main=(toMain *)qApp->mainWidget();
00895   if (main&&edit) {
00896     main->Edit=edit;
00897     main->RowLabel->hide();
00898     main->ColumnLabel->hide();
00899     main->editEnable(edit);
00900   }
00901 }
00902 
00903 void toMain::editEnable(toEditWidget *edit)
00904 {
00905   toMain *main=(toMain *)qApp->mainWidget();
00906   if (main)
00907     main->editEnable(edit,
00908                      edit->openEnabled(),
00909                      edit->saveEnabled(),
00910                      edit->printEnabled(),
00911                      edit->undoEnabled(),
00912                      edit->redoEnabled(),
00913                      edit->cutEnabled(),
00914                      edit->copyEnabled(),
00915                      edit->pasteEnabled(),
00916                      edit->searchEnabled(),
00917                      edit->selectAllEnabled(),
00918                      edit->readAllEnabled());
00919 
00920 #if QT_VERSION >= 300
00921   // Set Selection Mode on X11
00922   QClipboard *clip=qApp->clipboard();
00923   if(clip->supportsSelection())
00924     clip->setSelectionMode(true);
00925 #endif
00926 }
00927 
00928 void toMain::editDisable(toEditWidget *edit)
00929 {
00930   toMain *main=(toMain *)qApp->mainWidget();
00931 
00932   if (main) {
00933     if(edit&&edit==main->Edit) {
00934       main->editEnable(edit,false,false,false,false,false,false,false,false,false,false,false);
00935       main->Edit=NULL;
00936     }
00937   }
00938 }
00939 
00940 toEditWidget *toMain::findEdit(QWidget *widget)
00941 {
00942   while(widget) {
00943     toEditWidget *edit=dynamic_cast<toEditWidget *>(widget);
00944     if (edit)
00945       return edit;
00946     widget=widget->parentWidget();
00947   }
00948   return NULL;
00949 }
00950 
00951 void toMain::editEnable(toEditWidget *edit,bool open,bool save,bool print,
00952                         bool undo,bool redo,
00953                         bool cut,bool copy,bool paste,
00954                         bool,bool,bool)
00955 {
00956   if (edit&&edit==Edit) {
00957     LoadButton->setEnabled(open);
00958     if (save)
00959       SaveButton->setEnabled(true);
00960     else
00961       SaveButton->setEnabled(false);
00962 
00963     PrintButton->setEnabled(print);
00964 
00965     if (undo)
00966       UndoButton->setEnabled(true);
00967     else
00968       UndoButton->setEnabled(false);
00969     if (redo)
00970       RedoButton->setEnabled(true);
00971     else
00972       RedoButton->setEnabled(false);
00973 
00974     if (cut)
00975       CutButton->setEnabled(true);
00976     else
00977       CutButton->setEnabled(false);
00978     if (copy)
00979       CopyButton->setEnabled(true);
00980     else
00981       CopyButton->setEnabled(false);
00982 
00983     if (paste)
00984       PasteButton->setEnabled(true);
00985     else
00986       PasteButton->setEnabled(false);
00987   }
00988 }
00989 
00990 void toMain::registerSQLEditor(int tool)
00991 {
00992   SQLEditor=tool;
00993 }
00994 
00995 bool toMain::close(bool del)
00996 {
00997   // Workaround in bug in Qt 3.0.0
00998   while (workspace()->windowList().count()>0&&workspace()->windowList().at(0))
00999     if (workspace()->windowList().at(0)&&
01000         !workspace()->windowList().at(0)->close(true))
01001       return false;
01002   while (Connections.end()!=Connections.begin()) {
01003     if (!delConnection())
01004       return false;
01005   }
01006   return QMainWindow::close(del);
01007 }
01008 
01009 void toMain::createDefault(void)
01010 {
01011   commandCallback(DefaultTool);
01012 }
01013 
01014 void toMain::setCoordinates(int line,int col)
01015 {
01016   QString str("Row: ");
01017   str+=QString::number(line);
01018   RowLabel->setText(str);
01019   str="Col:";
01020   str+=QString::number(col);
01021   ColumnLabel->setText(str);
01022   RowLabel->show();
01023   ColumnLabel->show();
01024 }
01025 
01026 void toMain::editSQL(const QString &str)
01027 {
01028   if (SQLEditor>=0&&Tools[SQLEditor]) {
01029     Tools[SQLEditor]->createWindow();
01030     emit sqlEditor(str);
01031   }
01032 }
01033 
01034 void toMain::contextHelp(void)
01035 {
01036   toHelp::displayHelp();
01037 }
01038 
01039 void toMain::statusMenu(void)
01040 {
01041   std::list<QString> status=toStatusMessages();
01042   StatusMenu->clear();
01043   int id=TO_STATUS_ID;
01044   for(std::list<QString>::iterator i=status.begin();i!=status.end();i++)
01045     StatusMenu->insertItem(*i,id++);
01046 }
01047 
01048 void toMain::changeConnection(void)
01049 {
01050   toConnection &conn=currentConnection();
01051   for (std::map<QToolButton *,toTool *>::iterator i=NeedConnection.begin();
01052        i!=NeedConnection.end();i++) {
01053     toTool *tool=(*i).second;
01054     if (!tool)
01055       (*i).first->setEnabled(true);
01056     else if (tool->canHandle(conn))
01057       (*i).first->setEnabled(true);
01058     else
01059       (*i).first->setEnabled(false);
01060   }  
01061   for (std::map<int,toTool *>::iterator j=Tools.begin();j!=Tools.end();j++) {
01062     toTool *tool=(*j).second;
01063     if (!tool)
01064       menuBar()->setItemEnabled((*j).first,true);
01065     else if (tool->canHandle(conn))
01066       menuBar()->setItemEnabled((*j).first,true);
01067     else
01068       menuBar()->setItemEnabled((*j).first,false);
01069 
01070   }
01071 }
01072 
01073 void toMain::checkCaching(void)
01074 {
01075   int num=0;
01076   for(std::list<toConnection *>::iterator i=Connections.begin();i!=Connections.end();i++) {
01077     if (!(*i)->cacheAvailable(false,false))
01078       num++;
01079   }
01080   if (num==0)
01081     Poll.stop();
01082   else
01083     Poll.start(100);
01084 }
01085 
01086 void toMain::displayMessage(void)
01087 {
01088   static bool recursive=false;
01089   if (recursive)
01090     return;
01091   recursive=true;
01092 
01093   for(QString str=toShift(StatusMessages);!str.isEmpty();str=toShift(StatusMessages)) {
01094     toMessageUI dialog(toMainWidget(),NULL,true);
01095     dialog.Message->setText(str);
01096     dialog.exec();
01097     if (dialog.Statusbar->isChecked()) {
01098       toTool::globalSetConfig(CONF_MESSAGE_STATUSBAR,"Yes");
01099       TOMessageBox::information(toMainWidget(),
01100                                 "Information","You can enable this through the Global Settings in the Options (Edit menu)");
01101       toTool::saveConfig();
01102     }
01103   }
01104   recursive=false;
01105 }
01106 
01107 void toMain::displayMessage(const QString &str)
01108 {
01109   toPush(StatusMessages,str);
01110   QTimer::singleShot(1,this,SLOT(displayMessage()));
01111 }

Generated on Sat Dec 15 00:00:58 2007 for BulmaGes by  doxygen 1.5.1