00001 00012 // for defects 00013 #ifdef _MSC_VER 00014 #include "msdevstudio/MSconfig.h" 00015 #endif 00016 00017 // for version 00018 #ifdef HAVE_CONFIG_H 00019 #include "config.h" 00020 #endif 00021 00022 #include "WindowController.h" 00023 00024 #include "CanvasView.h" 00025 #include "CanvasWindow.h" 00026 #include "Inspector.h" 00027 00028 #include <qapplication.h> 00029 #if QT_VERSION < 0x040000 00030 #else 00031 #include <QDesktopWidget> 00032 #endif 00033 00034 #include <algorithm> 00035 00036 using std::find; 00037 using std::list; 00038 using std::mem_fun; 00039 using std::for_each; 00040 00041 WindowController * WindowController::s_instance = 0; 00042 std::string WindowController::s_version ( VERSION ); 00043 00044 /* static */ 00045 const std::string & WindowController::version () 00046 { 00047 return s_version; 00048 } 00049 00050 WindowController::WindowController ( ) 00051 : m_active_window ( 0 ), 00052 m_inspector ( 0 ), 00053 m_quit ( false ) 00054 { 00055 s_instance = this; 00056 } 00057 00058 WindowController::~WindowController () 00059 { 00060 } 00061 00062 WindowController * WindowController::instance () 00063 { 00064 if ( s_instance == 0 ) { 00065 s_instance = new WindowController (); 00066 } 00067 00068 return s_instance; 00069 } 00070 00071 void WindowController::createInspector () 00072 { 00073 if ( m_inspector == 0 ) { 00074 m_inspector = new Inspector (); 00075 } 00076 positionInspector (); 00077 } 00078 00079 void WindowController::newWindow ( CanvasWindow * window ) 00080 { 00081 unsigned int size = m_window_list.size (); 00082 if ( size == 0 && 00083 m_quit == true ) { 00084 window->inhibitClose (); 00085 } 00086 else if ( size == 1 ) { 00087 CanvasWindow * w = m_window_list.front (); 00088 w->inhibitClose ( false ); 00089 } 00090 00091 m_window_list.push_back ( window ); 00092 positionCanvas ( window ); 00093 if ( m_inspector != 0 ) resizeCanvas ( window ); 00094 setCurrentCanvas ( window ); 00095 00096 CanvasView * view = window->getCanvasView (); 00097 if ( m_inspector != 0 ) view->setInspector ( m_inspector ); 00098 00099 } 00100 00101 void 00102 WindowController:: 00103 quitOnLastWindowClose ( bool yes ) 00104 { 00105 m_quit = yes; 00106 } 00107 00108 void WindowController::aboutToClose ( CanvasWindow * window ) 00109 { 00110 m_window_list.remove ( window ); 00111 // window will delete itself. 00112 00113 if ( m_window_list.size() == 1 && 00114 m_quit == true ) { 00115 CanvasWindow * w = m_window_list.front(); 00116 w->inhibitClose (); 00117 } 00118 if ( m_window_list.empty() ) { 00119 if ( m_inspector != 0 ) { 00120 m_inspector -> hide (); 00121 if ( m_quit == true ) { 00122 m_inspector -> close (); 00123 m_inspector = 0; 00124 } 00125 qApp->quit (); 00126 } 00127 } 00128 } 00129 00130 void WindowController::hasBeenHidden ( CanvasWindow * window ) 00131 { 00132 bool allhidden = true; 00133 #ifdef ITERATOR_MEMBER_DEFECT 00134 std:: 00135 #endif 00136 list < CanvasWindow * > :: iterator first = m_window_list.begin (); 00137 while ( first != m_window_list.end() ) { 00138 CanvasWindow * w = *first++; 00139 allhidden = w->isMinimized (); 00140 if ( allhidden == false ) break; 00141 } 00142 if ( allhidden == true ) { 00143 if ( m_inspector != 0 ) m_inspector->hide(); 00144 } 00145 } 00146 00147 void WindowController::unHide ( CanvasWindow * ) 00148 { 00149 if ( m_inspector != 0 ) m_inspector->show(); 00150 } 00151 00152 bool WindowController::okToQuit () 00153 { 00154 #ifdef ITERATOR_MEMBER_DEFECT 00155 std:: 00156 #endif 00157 list < CanvasWindow * > :: iterator first = m_window_list.begin (); 00158 bool ok = m_quit; 00159 00160 if ( m_quit == true ) { 00161 while ( first != m_window_list.end() ) { 00162 CanvasWindow * window = *first++; 00163 ok = window->allowClose (); 00164 if ( ! ok ) break; 00165 } 00166 } 00167 00168 return ok; 00169 } 00170 00171 void 00172 WindowController:: 00173 updateActions () 00174 { 00175 for_each ( m_window_list.begin(), m_window_list.end(), 00176 mem_fun ( &CanvasWindow::updateActions ) ); 00177 } 00178 00179 void WindowController::closeAllWindows () 00180 { 00181 #ifdef ITERATOR_MEMBER_DEFECT 00182 std:: 00183 #endif 00184 list < CanvasWindow * > :: iterator first = m_window_list.begin (); 00185 while ( first != m_window_list.end() ) { 00186 CanvasWindow * window = *first++; 00187 window->close ( ); 00188 } 00189 00190 m_inspector->close(); 00191 } 00192 00193 #if QT_VERSION < 0x040000 00194 const QRect & WindowController::getScreenRect () 00195 #else 00196 QRect WindowController::getScreenRect () 00197 #endif 00198 { 00199 QDesktopWidget * dt = QApplication::desktop (); 00200 int screen = dt->primaryScreen (); 00201 00202 return dt->screenGeometry ( screen ); 00203 } 00204 00205 void WindowController::positionCanvas ( CanvasWindow * window ) 00206 { 00207 const QRect & rect = getScreenRect (); 00208 QSize size = rect.size (); 00209 size *= 0.95; 00210 window->setMaximumSize ( size ); 00211 00212 QPoint p = rect.topLeft (); 00213 if ( m_active_window != 0 ) { 00214 p = m_active_window->pos(); 00215 QPoint offset ( 10, 10 ); 00216 p += offset; 00217 } 00218 00219 window->move ( p ); 00220 } 00221 void 00222 WindowController:: 00223 resizeCanvas ( CanvasWindow * window ) 00224 { 00225 if ( m_inspector == 0 ) return; // can't do it without inspector 00226 00227 QPoint insp_pos = m_inspector -> pos (); 00228 QSize w_size = window -> size (); 00229 00230 const QRect & rect = getScreenRect (); 00231 QSize s_size = rect.size (); 00232 s_size *= 0.95; 00233 QSize size = window -> sizeHint (); 00234 size.setHeight ( s_size.height () ); 00235 00236 if ( w_size.width() > insp_pos.x () ) { 00237 double x = insp_pos.x (); 00238 double w = w_size.width (); 00239 double scale = 0.90 * ( x / w ); 00240 CanvasView * cv = window -> getCanvasView (); 00241 cv -> scaleBy ( scale ); 00242 00243 QSize size = window -> sizeHint (); 00244 int width = static_cast < int > ( 0.95 * insp_pos.x () ); 00245 size.setWidth ( width ); 00246 window -> resize ( size ); 00247 } 00248 } 00249 00250 void WindowController::positionInspector ( ) 00251 { 00252 const QRect & rect = getScreenRect (); 00253 QPoint pos = m_inspector->pos(); 00254 QSize size = m_inspector->size (); 00255 int new_x = rect.width() - size.width(); 00256 pos.setX ( new_x ); 00257 00258 m_inspector->move ( pos ); 00259 } 00260 00261 void WindowController::setCurrentCanvas ( CanvasWindow * canvas ) 00262 { 00263 m_active_window = canvas; 00264 } 00265 00266 CanvasWindow * WindowController::currentCanvas () 00267 { 00268 return m_active_window; 00269 } 00270 00271 Inspector * WindowController::getInspector () 00272 { 00273 return m_inspector; 00274 } 00275 00276 void 00277 WindowController:: 00278 setFirstWindow () 00279 { 00280 createInspector (); 00281 quitOnLastWindowClose (); 00282 CanvasWindow * window = new CanvasWindow (); 00283 window->setCaption (); 00284 00285 window->show(); 00286 }