lib Library API Documentation

kotooldockmovemanager.cpp

00001 /*
00002  * This file is part of the KDE project
00003   Copyright (C) 2000-2001 theKompany.com & Dave Marotti
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00018  */
00019 #include "kotooldockmovemanager.h"
00020 
00021 #include <qcursor.h>
00022 #include <qapplication.h>
00023 #include <qwindowdefs.h>
00024 #include <qtimer.h>
00025 #include <qrect.h>
00026 #include <qframe.h>
00027 #include <qpainter.h>
00028 
00029 KoToolDockMoveManager::KoToolDockMoveManager()
00030     : QObject()
00031 {
00032 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00033     XGCValues gv;
00034 #endif
00035 
00036     working=false;
00037     noLast=true;
00038 
00039 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00040     scr = qt_xscreen();
00041     root = qt_xrootwin();
00042 
00043     gv.function = GXxor;
00044     gv.line_width = 2;
00045     gv.foreground = WhitePixel(qt_xdisplay(), scr)^BlackPixel(qt_xdisplay(), scr);
00046     gv.subwindow_mode = IncludeInferiors;
00047     long mask = GCForeground | GCFunction | GCLineWidth | GCSubwindowMode;
00048     rootgc = XCreateGC(qt_xdisplay(), qt_xrootwin(), mask, &gv);
00049 #endif
00050 
00051     timer = new QTimer(this);
00052 }
00053 
00054 KoToolDockMoveManager::~KoToolDockMoveManager()
00055 {
00056     stop();
00057     delete timer;
00058 }
00059 
00060 void KoToolDockMoveManager::doMove( QWidget* _w )
00061 {
00062     if (working)
00063         return;
00064 
00065     working=true;
00066     isDoMove = true;
00067     mirrorX=false;
00068     mirrorY=false;
00069 
00070     setWidget(_w);
00071 
00072     offX = rx - p.x();
00073     offY = ry - p.y();
00074 
00075     orig_x = p.x();
00076     orig_y = p.y();
00077     orig_w = w;
00078     orig_h = h;
00079 
00080     QApplication::setOverrideCursor(QCursor(sizeAllCursor));
00081 
00082     movePause(false,false);
00083 
00084     drawRectangle(xp, yp, w, h);
00085 
00086     timer->disconnect();
00087     connect(timer,SIGNAL(timeout()),SLOT(doMoveInternal()));
00088     timer->start(0);
00089 }
00090 
00091 void KoToolDockMoveManager::doMoveInternal()
00092 {
00093     if ( !working )
00094         return;
00095 
00096     if (!pauseMoveX)
00097         rx = QCursor::pos().x();
00098 
00099     if (!pauseMoveY)
00100         ry = QCursor::pos().y();
00101 
00102     xp = rx - offX;
00103     yp = ry - offY;
00104 
00105     emit positionChanged();
00106 
00107     if (check(xp, yp, w, h)) {
00108         paintProcess(false,xp, yp, w, h);
00109 
00110 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00111         XFlush(qt_xdisplay());
00112         XSync(qt_xdisplay(),false);
00113 #endif
00114     }
00115 }
00116 
00117 void KoToolDockMoveManager::stop()
00118 {
00119     if (!working)
00120         return;
00121 
00122     timer->stop();
00123     QApplication::restoreOverrideCursor();
00124 
00125     paintProcess();
00126 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00127     XFlush(qt_xdisplay());
00128 #endif
00129 
00130     working = false;
00131 }
00132 
00133 void KoToolDockMoveManager::setGeometry( const QRect& r )
00134 {
00135     setGeometry(r.x(),r.y(),r.width(),r.height());
00136 }
00137 
00138 void KoToolDockMoveManager::setGeometry(int _x, int _y, int _w, int _h)
00139 {
00140     xp=_x;
00141     yp=_y;
00142     w=_w;
00143     h=_h;
00144 
00145     check(_x, _y, _w, _h, true);
00146     paintProcess(false,_x, _y, _w, _h);
00147 
00148 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00149     XFlush(qt_xdisplay());
00150     XSync(qt_xdisplay(),false);
00151 #endif
00152 }
00153 
00154 void KoToolDockMoveManager::drawRectangle( int _x, int _y, int _w, int _h)
00155 {
00156     if (!noLast)
00157         return;
00158 
00159     ox = _x;
00160     oy = _y;
00161     ow = _w;
00162     oh = _h;
00163 
00164 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00165     XDrawRectangle(qt_xdisplay(), root, rootgc, _x, _y, _w, _h);
00166 #endif
00167     noLast = false;
00168 }
00169 
00170 void KoToolDockMoveManager::paintProcess( bool onlyDelete, int _x, int _y, int _w, int _h )
00171 {
00172     if (noLast && onlyDelete)
00173         return;
00174 
00175     if ( ox == _x && oy == _y && ow ==_w && oh == _h )
00176         return;
00177 
00178 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00179     XDrawRectangle(qt_xdisplay(), root, rootgc, ox, oy, ow, oh);
00180 #endif
00181     noLast = true;
00182 
00183     drawRectangle(_x,_y,_w,_h);
00184 }
00185 
00186 void KoToolDockMoveManager::movePause( bool horizontal, bool vertical )
00187 {
00188     pauseMoveX = horizontal;
00189     pauseMoveY = vertical;
00190 }
00191 
00192 void KoToolDockMoveManager::moveContinue()
00193 {
00194     pauseMoveX = false;
00195     pauseMoveY = false;
00196 }
00197 
00198 void KoToolDockMoveManager::doXResize( QWidget* w, bool mirror )
00199 {
00200     if (working)
00201         return;
00202 
00203     mirrorX = mirror;
00204     mirrorY = false;
00205 
00206     yOnly = false;
00207     xOnly = true;
00208 
00209     doResize(w);
00210 }
00211 
00212 void KoToolDockMoveManager::doYResize( QWidget* w, bool mirror )
00213 {
00214     if (working)
00215         return;
00216 
00217     mirrorX = false;
00218     mirrorY = mirror;
00219 
00220     yOnly = true;
00221     xOnly = false;
00222 
00223     doResize(w);
00224 }
00225 
00226 void KoToolDockMoveManager::doXYResize( QWidget* w, bool _mirrorX, bool _mirrorY )
00227 {
00228     if (working)
00229         return;
00230 
00231     mirrorX = _mirrorX;
00232     mirrorY = _mirrorY;
00233 
00234     yOnly = false;
00235     xOnly = false;
00236 
00237     doResize(w);
00238 }
00239 
00240 void KoToolDockMoveManager::doResizeInternal()
00241 {
00242     if (!yOnly)
00243         rx = QCursor::pos().x();
00244 
00245     if (!xOnly)
00246         ry = QCursor::pos().y();
00247 
00248     int dx = rx - sx;
00249     int dy = ry - sy;
00250 
00251     if ( mirrorX ){
00252         w = rr.width() - dx;
00253         xp = rr.x() + dx;
00254     } else {
00255         w = rr.width() + dx;
00256     }
00257 
00258     if ( mirrorY ){
00259         h = rr.height() - dy;
00260         yp = rr.y() + dy;
00261     } else {
00262         h = rr.height() + dy;
00263     }
00264 
00265     emit sizeChanged();
00266 
00267    if (check(xp, yp, w, h)) {
00268         paintProcess(false,xp, yp, w, h);
00269 
00270 #if defined Q_WS_X11 && !defined K_WS_QTONLY
00271         XFlush(qt_xdisplay());
00272         XSync(qt_xdisplay(),false);
00273 #endif
00274     }
00275 }
00276 
00277 void KoToolDockMoveManager::setWidget( QWidget* _w )
00278 {
00279     widget = _w;
00280     minSize = widget->minimumSize();
00281     maxSize = widget->maximumSize();
00282 
00283     rr = QRect(widget->mapToGlobal(QPoint(0,0)),widget->size());
00284     p =  rr.topLeft();
00285 
00286     xp = rr.x();
00287     yp = rr.y();
00288     w  = rr.width();
00289     h  = rr.height();
00290 
00291     rx = QCursor::pos().x();
00292     ry = QCursor::pos().y();
00293 }
00294 
00295 void KoToolDockMoveManager::doResize( QWidget* _w )
00296 {
00297     setWidget(_w);
00298 
00299     working=true;
00300     isDoMove = false;
00301 
00302     QPoint curPos = QCursor::pos();
00303     sx = curPos.x();
00304     sy = curPos.y();
00305 
00306     offX = sx - p.x();
00307     offY = sy - p.y();
00308 
00309     drawRectangle(xp, yp, w, h);
00310 
00311     timer->disconnect();
00312     connect(timer,SIGNAL(timeout()),SLOT(doResizeInternal()));
00313     timer->start(0);
00314 }
00315 
00316 bool KoToolDockMoveManager::check(int& x, int& y, int& w, int& h, bool change)
00317 {
00318 
00319     int w1 = QMIN(QMAX(minSize.width(), w), maxSize.width());
00320     int h1 = QMIN(QMAX(minSize.height(), h), maxSize.height());
00321 
00322     bool f1 = (w1-w)+(h1-h) == 0;
00323 
00324     if (change) {
00325         if (mirrorX)
00326             x += w - w1;
00327         w = w1;
00328         if (mirrorY)
00329             y += h - h1;
00330         h = h1;
00331     }
00332 
00333     int x0 = x;
00334     int y0 = y;
00335     int w0 = w;
00336     int h0 = h;
00337 
00338     if (isDoMove)
00339         emit fixPosition(x0,y0,w0,h0);
00340     else
00341         emit fixSize(x0,y0,w0,h0);
00342 
00343     bool f2 = (x0==x)&&(y0==y)&&(w0==w)&&(h0==h);
00344 
00345     if (change) {
00346         x = x0;
00347         y = y0;
00348         w = w0;
00349         h = h0;
00350     }
00351 
00352     return f1&&f2;
00353 }
00354 
00355 QRect KoToolDockMoveManager::geometry()
00356 {
00357     int x0 = xp;
00358     int y0 = yp;
00359     int w0 = w;
00360     int h0 = h;
00361     check(x0,y0,w0,h0,true);
00362 
00363     return QRect(x0,y0,w0,h0);
00364 }
00365 #include "kotooldockmovemanager.moc"
KDE Logo
This file is part of the documentation for lib Library Version 1.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Mon Feb 13 09:40:13 2006 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003