00001
00012
00013 #ifdef _MSC_VER
00014 #include "msdevstudio/MSconfig.h"
00015 #include "Windows.h"
00016 #endif
00017
00018
00019 #ifdef HAVE_CONFIG_H // for VERSION
00020 #include "config.h"
00021 #endif
00022
00023 #include "WindowController.h"
00024
00025 #include "CanvasView.h"
00026 #include "CanvasWindow.h"
00027 #include "Inspector.h"
00028
00029 #include <qapplication.h>
00030 #if QT_VERSION < 0x040000
00031 #else
00032 #include <QtGui/QDesktopWidget>
00033 #endif
00034
00035 #if QT_VERSION < 0x040000
00036 #include <qassistantclient.h>
00037 #else
00038 #include <QtAssistant/QAssistantClient>
00039 #endif
00040
00041 #include <qdir.h>
00042
00043 #include <algorithm>
00044
00045 using std::find;
00046 using std::list;
00047 using std::mem_fun;
00048 using std::for_each;
00049
00050 using namespace hippodraw;
00051
00052 WindowController * WindowController::s_instance = 0;
00053 std::string WindowController::s_version ( VERSION );
00054
00055 const std::string & WindowController::version ()
00056 {
00057 return s_version;
00058 }
00059
00060 WindowController::WindowController ( )
00061 : m_active_window ( 0 ),
00062 m_inspector ( 0 ),
00063 m_assistant ( 0 ),
00064 m_quit ( true )
00065 {
00066 s_instance = this;
00067 }
00068
00069 WindowController::~WindowController ()
00070 {
00071 s_instance = 0;
00072 }
00073
00074 WindowController * WindowController::instance ()
00075 {
00076 if ( s_instance == 0 ) {
00077 s_instance = new WindowController ();
00078 }
00079
00080 return s_instance;
00081 }
00082
00083 void WindowController::createInspector ()
00084 {
00085 if ( m_inspector == 0 ) {
00086 m_inspector = new Inspector ();
00087 }
00088 positionInspector ();
00089 }
00090
00091 void WindowController::newWindow ( CanvasWindow * window )
00092 {
00093 unsigned int size = m_window_list.size ();
00094 if ( size == 0 &&
00095 m_quit == true ) {
00096 window->inhibitClose ();
00097 }
00098 else if ( size == 1 ) {
00099 CanvasWindow * w = m_window_list.front ();
00100 w->inhibitClose ( false );
00101 }
00102
00103 m_window_list.push_back ( window );
00104 positionCanvas ( window );
00105 if ( m_inspector != 0 ) resizeCanvas ( window );
00106 setCurrentCanvas ( window );
00107
00108 CanvasView * view = window->getCanvasView ();
00109 if ( m_inspector != 0 ) {
00110 view->setInspector ( m_inspector );
00111 moveInspector ( window );
00112 }
00113
00114 }
00115
00116 void
00117 WindowController::
00118 moveInspector ( CanvasWindow * window )
00119 {
00120 if ( m_inspector != 0 ) {
00121 QPoint w_pos = window -> pos ();
00122 QSize w_size = window -> size ();
00123 int pos = w_pos.x() + static_cast < int > ( 1.05 * w_size.width () );
00124 w_pos.setX ( pos );
00125 m_inspector -> move ( w_pos );
00126 }
00127 }
00128
00129 void
00130 WindowController::
00131 quitOnLastWindowClose ( bool yes )
00132 {
00133 m_quit = yes;
00134 }
00135
00139 void
00140 WindowController::
00141 aboutToClose ( CanvasWindow * window )
00142 {
00143 m_window_list.remove ( window );
00144
00145
00146 if ( m_window_list.size() == 1 &&
00147 m_quit == true ) {
00148 CanvasWindow * w = m_window_list.front();
00149 w->inhibitClose ();
00150 }
00151 if ( m_window_list.empty() ) {
00152 if ( m_inspector != 0 ) {
00153 m_inspector -> hide ();
00154 if ( m_quit == true ) {
00155 m_inspector -> close ();
00156 m_inspector = 0;
00157 }
00158 window->autosaveSettings();
00159 m_window_list.clear ();
00160 qApp->quit ();
00161 }
00162 }
00163 }
00164
00165 void WindowController::hasBeenHidden ()
00166 {
00167 bool allhidden = true;
00168 #ifdef ITERATOR_MEMBER_DEFECT
00169 std::
00170 #endif
00171 list < CanvasWindow * > :: iterator first = m_window_list.begin ();
00172 while ( first != m_window_list.end() ) {
00173 CanvasWindow * w = *first++;
00174 allhidden = w->isMinimized ();
00175 if ( allhidden == false ) break;
00176 }
00177 if ( allhidden == true ) {
00178 if ( m_inspector != 0 ) m_inspector->hide();
00179 }
00180 }
00181
00182 void WindowController::unHide ( CanvasWindow * )
00183 {
00184 if ( m_inspector != 0 ) m_inspector->show();
00185 }
00186
00187 bool WindowController::okToQuit ()
00188 {
00189 #ifdef ITERATOR_MEMBER_DEFECT
00190 std::
00191 #endif
00192 list < CanvasWindow * > :: iterator first = m_window_list.begin ();
00193 bool ok = m_quit;
00194
00195 if ( m_quit == true ) {
00196 while ( first != m_window_list.end() ) {
00197 CanvasWindow * window = *first++;
00198 ok = window->allowClose ();
00199 if ( ! ok ) break;
00200 }
00201 }
00202
00203 return ok;
00204 }
00205
00206 void
00207 WindowController::
00208 updateActions ()
00209 {
00210 if ( m_window_list.empty() == false ) {
00211 for_each ( m_window_list.begin(), m_window_list.end(),
00212 mem_fun ( &CanvasWindow::updateActions ) );
00213 }
00214 }
00215
00216 void WindowController::closeAllWindows ( bool alsoDelete )
00217 {
00218 #ifdef ITERATOR_MEMBER_DEFECT
00219 std::
00220 #endif
00221 list < CanvasWindow * > :: iterator first = m_window_list.begin ();
00222 while ( first != m_window_list.end() ) {
00223 CanvasWindow * window = *first++;
00224 window -> setAllowClose ( true );
00225 window->close ( alsoDelete );
00226 }
00227
00228 if ( m_inspector != 0 ) m_inspector -> close ( alsoDelete);
00229 }
00230
00231 #if QT_VERSION < 0x040000
00232 const QRect & WindowController::getScreenRect ()
00233 #else
00234 QRect WindowController::getScreenRect ()
00235 #endif
00236 {
00237 QDesktopWidget * dt = QApplication::desktop ();
00238 int screen = dt->primaryScreen ();
00239
00240 return dt->screenGeometry ( screen );
00241 }
00242
00245 void WindowController::positionCanvas ( CanvasWindow * window )
00246 {
00247 const QRect & rect = getScreenRect ();
00248 QPoint p = rect.topLeft ();
00249 if ( m_active_window != 0 ) {
00250 p = m_active_window->pos();
00251 QPoint offset ( 10, 10 );
00252 p += offset;
00253 }
00254
00255 #ifdef __APPLE__
00256 p.setY ( p.y() + 23 );
00257 #endif
00258
00259 window->move ( p );
00260 }
00261 void
00262 WindowController::
00263 resizeCanvas ( CanvasWindow * window )
00264 {
00265 if ( m_inspector == 0 ) return;
00266
00267 QPoint insp_pos = m_inspector -> pos ();
00268 QSize w_size = window -> size ();
00269
00270 const QRect & rect = getScreenRect ();
00271 QSize s_size = rect.size ();
00272 s_size *= 0.95;
00273 QSize size = window -> sizeHint ();
00274 size.setHeight ( s_size.height () );
00275
00276 if ( w_size.width() > insp_pos.x () ) {
00277 double x = insp_pos.x ();
00278 double w = w_size.width ();
00279 double scale = 0.90 * ( x / w );
00280 CanvasView * cv = window -> getCanvasView ();
00281 cv -> scaleBy ( scale );
00282
00283 QSize size = window -> sizeHint ();
00284 int width = static_cast < int > ( 0.95 * insp_pos.x () );
00285 size.setWidth ( width );
00286 window -> resize ( size );
00287 }
00288 }
00289
00290 void WindowController::positionInspector ( )
00291 {
00292 const QRect & rect = getScreenRect ();
00293 QPoint pos = m_inspector->pos();
00294 QSize size = m_inspector->size ();
00295 int new_x = rect.width() - size.width();
00296 pos.setX ( new_x );
00297 #ifdef __APPLE__
00298 pos.setY ( 23 );
00299 #else
00300 pos.setY ( 0 );
00301 #endif
00302
00303 m_inspector->move ( pos );
00304 }
00305
00306 void WindowController::setCurrentCanvas ( CanvasWindow * canvas )
00307 {
00308 m_active_window = canvas;
00309 }
00310
00311 CanvasWindow * WindowController::currentCanvas ()
00312 {
00313 return m_active_window;
00314 }
00315
00316 Inspector * WindowController::getInspector ()
00317 {
00318 return m_inspector;
00319 }
00320
00321 void
00322 WindowController::
00323 setFirstWindow ()
00324 {
00325 createInspector ();
00326 quitOnLastWindowClose ();
00327 CanvasWindow * window = new CanvasWindow ();
00328 window->setCaption ();
00329
00330 window->show();
00331 }
00332
00333 void
00334 WindowController::
00335 openAssistant ()
00336 {
00337 #ifdef HAVE_HELP
00338 #ifdef _MSC_VER
00339 QString exe_path;
00340 #endif
00341 if ( m_assistant == 0 ) {
00342 #ifdef _MSC_VER
00343
00344
00345 LPCTSTR module = "qthippo.dll";
00346 HMODULE handle = GetModuleHandle ( module );
00347 int size = MAX_PATH;
00348 TCHAR path[MAX_PATH];
00349 DWORD count = GetModuleFileName ( handle, path, size );
00350 exe_path = QDir ( path ).absPath();
00351
00352 int pos = exe_path.findRev ( QString ( module ) );
00353 exe_path.remove ( module );
00354 unsigned int len = exe_path.length();
00355 exe_path.truncate( len -1 );
00356 CanvasWindow * current = currentCanvas();
00357 m_assistant = new QAssistantClient( exe_path , current );
00358 #else
00359 CanvasWindow * current = currentCanvas();
00360
00361 m_assistant = new QAssistantClient( QString ( QTDIR ), current );
00362 #endif
00363 }
00364
00365 #ifdef _MSC_VER
00366 QString docs_path = exe_path + "/doc";
00367 #else
00368
00369 QString docs_path = QDir ( DOCS ).absPath();
00370 #endif
00371
00372 #if QT_VERSION < 0x030200
00373 #else
00374 if ( m_assistant -> isOpen() == false ) {
00375 QString adp = docs_path + QString ( "/hippodraw.adp" );
00376 QStringList args;
00377 args << "-profile"
00378 << adp;
00379 m_assistant -> setArguments ( args );
00380 }
00381 #endif
00382 QString page ( "%1/assist_root.html" );
00383 page = page.arg ( docs_path );
00384 m_assistant ->
00385 showPage ( page );
00386 #endif // HAVE_HELP
00387 }