]> SALOME platform Git repositories - modules/gui.git/blob - src/Qtx/QtxWorkstack.cxx
Salome HOME
It is recommended to use standard Qt QInputDialog instead QtxNameDlg
[modules/gui.git] / src / Qtx / QtxWorkstack.cxx
1 // File:      QtxWorkstack.cxx
2 // Author:    Sergey TELKOV
3
4 #include "QtxWorkstack.h"
5
6 #include <qstyle.h>
7 #include <qimage.h>
8 #include <qaction.h>
9 #include <qlayout.h>
10 #include <qpixmap.h>
11 #include <qiconset.h>
12 #include <qpainter.h>
13 #include <qsplitter.h>
14 #include <qpopupmenu.h>
15 #include <qobjectlist.h>
16 #include <qpushbutton.h>
17 #include <qwidgetstack.h>
18 #include <qapplication.h>
19 #include <qinputdialog.h>
20
21 /*!
22     Class: QtxWorkstack [Public]
23     Descr:
24 */
25
26 QtxWorkstack::QtxWorkstack( QWidget* parent )
27 : QWidget( parent ),
28 myWin( 0 ),
29 myArea( 0 ),
30 myWorkWin( 0 )
31 {
32   myActionsMap.insert( SplitVertical,   new QAction( tr( "Split vertically" ),   0, this ) );
33   myActionsMap.insert( SplitHorizontal, new QAction( tr( "Split horizontally" ), 0, this ) );
34   myActionsMap.insert( Close,           new QAction( tr( "Close" ),       0, this ) );
35   myActionsMap.insert( Rename,          new QAction( tr( "Rename" ),      0, this ) );
36
37   connect( myActionsMap[SplitVertical], SIGNAL( activated() ), this, SLOT( splitVertical() ) );
38   connect( myActionsMap[SplitHorizontal], SIGNAL( activated() ), this, SLOT( splitHorizontal() ) );
39   connect( myActionsMap[Close], SIGNAL( activated() ), this, SLOT( onCloseWindow() ) );
40   connect( myActionsMap[Rename], SIGNAL( activated() ), this, SLOT( onRename() ) );
41
42   QVBoxLayout* base = new QVBoxLayout( this );
43   mySplit = new QSplitter( this );
44   mySplit->setChildrenCollapsible( false );
45   base->addWidget( mySplit );
46 }
47
48 QtxWorkstack::~QtxWorkstack()
49 {
50 }
51
52 QWidgetList QtxWorkstack::windowList() const
53 {
54   QPtrList<QtxWorkstackArea> lst;
55   areas( mySplit, lst, true );
56
57   QWidgetList widList;
58   for ( QPtrListIterator<QtxWorkstackArea> it( lst ); it.current(); ++it )
59   {
60     QWidgetList wids = it.current()->widgetList();
61     for ( QWidgetListIt itr( wids ); itr.current(); ++itr )
62       widList.append( itr.current() );
63   }
64
65   return widList;
66 }
67
68 QWidgetList QtxWorkstack::splitWindowList() const
69 {
70   return myArea ? myArea->widgetList() : QWidgetList();
71 }
72
73 QWidget* QtxWorkstack::activeWindow() const
74 {
75   return myWin;
76 }
77
78 void QtxWorkstack::split( const int o )
79 {
80   QtxWorkstackArea* area = activeArea();
81   if ( !area )
82     return;
83
84   if ( area->widgetList().count() < 2 )
85     return;
86
87   QWidget* curWid = area->activeWidget();
88   if ( !curWid )
89     return;
90
91   QSplitter* s = splitter( area );
92   QPtrList<QtxWorkstackArea> areaList;
93   areas( s, areaList );
94
95   QPtrList<QSplitter> splitList;
96   splitters( s, splitList );
97
98   QSplitter* trg = 0;
99   if ( areaList.count() + splitList.count() < 2 || s->orientation() == o )
100     trg = s;
101
102   if ( !trg )
103     trg = wrapSplitter( area );
104
105   if ( !trg )
106     return;
107
108   trg->setOrientation( (Orientation)o );
109
110   QtxWorkstackArea* newArea = createArea( 0 );
111   insertWidget( newArea, trg, area );
112
113   area->removeWidget( curWid );
114   newArea->insertWidget( curWid );
115
116   distributeSpace( trg );
117
118   curWid->show();
119   curWid->setFocus();
120 }
121
122 /*!
123 * \brief Split workarea of the given widget on two parts.
124 * \param wid  - widget, belonging to this workstack
125 * \param o    - orientation of splitting (Qt::Horizontal or Qt::Vertical)
126 * \param type - type of splitting, see <VAR>SplitType</VAR> enumeration
127 */
128 void QtxWorkstack::Split (QWidget* wid, const Qt::Orientation o, const SplitType type)
129 {
130   if (!wid) return;
131
132   // find area of the given widget
133   QtxWorkstackArea* area = NULL;
134   QPtrList<QtxWorkstackArea> allAreas;
135   areas(mySplit, allAreas, true);
136
137   QPtrListIterator<QtxWorkstackArea> it (allAreas);
138   for (; it.current() && !area; ++it) {
139     if (it.current()->contains(wid))
140       area = it.current();
141   }
142   if (!area) return;
143
144   QWidgetList wids = area->widgetList();
145   if (wids.count() < 2)
146     return;
147
148   QSplitter* s = splitter(area);
149   QPtrList<QtxWorkstackArea> areaList;
150   areas(s, areaList);
151
152   QPtrList<QSplitter> splitList;
153   splitters(s, splitList);
154
155   QSplitter* trg = 0;
156   if (areaList.count() + splitList.count() < 2 || s->orientation() == o)
157     trg = s;
158
159   if (!trg) trg = wrapSplitter(area);
160   if (!trg) return;
161
162   trg->setOrientation(o);
163
164   QtxWorkstackArea* newArea = createArea(0);
165   insertWidget(newArea, trg, area);
166
167   switch (type) {
168   case SPLIT_STAY:
169     {
170       QWidgetListIt itr (wids);
171       for (; itr.current(); ++itr)
172       {
173         QWidget* wid_i = itr.current();
174         if (wid_i != wid) {
175           area->removeWidget(wid_i);
176           newArea->insertWidget(wid_i);
177         }
178       }
179     }
180     break;
181   case SPLIT_AT:
182     {
183       QWidgetListIt itr (wids);
184       for (; itr.current() && itr.current() != wid; ++itr) {
185       }
186       for (; itr.current(); ++itr) {
187         area->removeWidget(itr.current());
188         newArea->insertWidget(itr.current());
189       }
190     }
191     break;
192   case SPLIT_MOVE:
193     area->removeWidget(wid);
194     newArea->insertWidget(wid);
195     break;
196   }
197
198   distributeSpace(trg);
199 }
200
201 /*!
202 * \brief Put given widget on top of its workarea
203 * \param wid - widget, belonging to this workstack
204 */
205 /*
206 void QtxWorkstack::OnTop (QWidget* wid)
207 {
208   if ( !wid )
209     return;
210
211   // find area of the given widget
212   QtxWorkstackArea* area = 0;
213   QPtrList<QtxWorkstackArea> allAreas;
214   areas( mySplit, allAreas, true );
215   for ( QPtrListIterator<QtxWorkstackArea> it( allAreas ); it.current() && !area; ++it )
216   {
217     if ( it.current()->contains( wid ) )
218       area = it.current();
219   }
220
221   if ( area )
222     area->setActiveWidget( wid );
223 }
224 */
225
226 /*!
227 * \brief Move widget(s) from source workarea into target workarea
228 *        or just reorder widgets inside one workarea.
229 * \param wid1 - widget from target workarea
230 * \param wid2 - widget from source workarea
231 * \param all  - if this parameter is TRUE, all widgets from source workarea will
232 *               be moved into the target one, else only the \a wid2 will be moved
233 *
234 * Move \a wid2 in target workarea. Put it right after \a wid1.
235 * If value of boolean argument is TRUE, all widgets from source workarea
236 * will be moved together with \a wid2, source workarea will be deleted.
237 * If \a wid1 and \a wid2 belongs to one workarea, simple reordering will take place.
238 */
239 void QtxWorkstack::Attract ( QWidget* wid1, QWidget* wid2, const bool all )
240 {
241   if ( !wid1 || !wid2 )
242     return;
243
244   // find area of the widgets
245   QtxWorkstackArea *area1 = NULL, *area2 = NULL;
246   QPtrList<QtxWorkstackArea> allAreas;
247   areas(mySplit, allAreas, true);
248   QPtrListIterator<QtxWorkstackArea> it (allAreas);
249   for (; it.current() && !(area1 && area2); ++it) {
250     if (it.current()->contains(wid1))
251       area1 = it.current();
252     if (it.current()->contains(wid2))
253       area2 = it.current();
254   }
255   if (!area1 || !area2) return;
256
257   QWidget* curWid = area1->activeWidget();
258   if (!curWid) return;
259
260   if (area1 == area2) {
261     if (all) {
262       // Set wid1 at first position, wid2 at second
263       area1->insertWidget(wid1);
264       area1->insertWidget(wid2, 1);
265     } else {
266       // Set wid2 right after wid1
267       area1->removeWidget(wid2);
268       int wid1_ind = 0;
269       QWidgetList wids1 = area1->widgetList();
270       QWidgetListIt itr1 (wids1);
271       for (; itr1.current() && itr1.current() != wid1; ++itr1, ++wid1_ind);
272       area1->insertWidget(wid2, wid1_ind + 1);
273     }
274   } else {
275     int wid1_ind = 0;
276     QWidgetList wids1 = area1->widgetList();
277     QWidgetListIt itr1 (wids1);
278     for (; itr1.current() && itr1.current() != wid1; ++itr1, ++wid1_ind);
279
280     if (all) {
281       // Set wid2 right after wid1, other widgets from area2 right after wid2
282       QWidgetList wids2 = area2->widgetList();
283       QWidgetListIt itr2 (wids2);
284       for (int ind = wid1_ind + 1; itr2.current(); ++itr2, ++ind)
285       {
286         area2->removeWidget(itr2.current());
287         if (itr2.current() == wid2) {
288           area1->insertWidget(itr2.current(), wid1_ind + 1);
289         } else {
290           area1->insertWidget(itr2.current(), ind);
291         }
292       }
293     } else {
294       // Set wid2 right after wid1
295       area2->removeWidget(wid2);
296       area1->insertWidget(wid2, wid1_ind + 1);
297     }
298   }
299
300   area1->setActiveWidget( curWid );
301 }
302
303 static void setSizes (QIntList& szList, const int item_ind,
304                       const int new_near, const int new_this, const int new_farr)
305 {
306   // set size to all items before an item # <item_ind>
307   int cur_pos = 0;
308   QIntList::iterator its = szList.begin();
309   for (; its != szList.end() && cur_pos < item_ind; ++its, ++cur_pos) {
310     *its = new_near;
311   }
312   if (its == szList.end()) return;
313   // set size to item # <item_ind>
314   *its = new_this;
315   ++its;
316   // set size to all items after an item # <item_ind>
317   for (; its != szList.end(); ++its) {
318     *its = new_farr;
319   }
320 }
321
322 /*!
323 * \brief Set position of the widget relatively its splitter.
324 * \param wid - widget to set position of
325 * \param pos - position relatively splitter. Value in range [0..1].
326 *
327 * Orientation of positioning will correspond to the splitter orientation.
328 */
329 void QtxWorkstack::SetRelativePositionInSplitter( QWidget* wid, const double position )
330 {
331   if ( position < 0.0 || 1.0 < position)
332     return;
333
334   if ( !wid )
335     return;
336
337   // find area of the given widget
338   QtxWorkstackArea* area = NULL;
339   QPtrList<QtxWorkstackArea> allAreas;
340   areas(mySplit, allAreas, true);
341   for ( QPtrListIterator<QtxWorkstackArea> it( allAreas );
342        it.current() && !area;
343        ++it )
344   {
345     if (it.current()->contains(wid))
346       area = it.current();
347   }
348
349   if ( !area )
350     return;
351
352   QSplitter* split = splitter( area );
353   if ( !split )
354     return;
355
356   // find index of the area in its splitter
357   int item_ind = -1;
358   bool isFound = false;
359   const QObjectList* was = split->children();
360   for (QObjectListIt ito (*was); ito.current() && !isFound; ++ito, ++item_ind)
361   {
362     if (ito.current() == area)
363       isFound = true;
364   }
365   if (!isFound || item_ind == 0)
366     return;
367
368   QIntList szList = split->sizes();
369   int splitter_size = (split->orientation() == Horizontal ?
370                        split->width() : split->height());
371   int nb = szList.count();
372
373   int new_prev = int(splitter_size * position / item_ind);
374   int new_next  = int(splitter_size * (1.0 - position) / (nb - item_ind));
375   setSizes (szList, item_ind, new_prev, new_next, new_next);
376   split->setSizes(szList);
377 }
378
379 /*!
380 * \brief Set position of the widget relatively the entire workstack.
381 * \param wid - widget to set position of
382 * \param o   - orientation of positioning (Qt::Horizontal or Qt::Vertical).
383 *              If o = Qt::Horizontal, horizontal position of \a wid will be changed.
384 *              If o = Qt::Vertical, vertical position of \a wid will be changed.
385 * \param pos - position relatively workstack. Value in range [0..1].
386 */
387 void QtxWorkstack::SetRelativePosition( QWidget* wid, const Qt::Orientation o,
388                                         const double position )
389 {
390   if ( position < 0.0 || 1.0 < position )
391     return;
392
393   if ( !wid )
394     return;
395
396   int splitter_size = o == Horizontal ? mySplit->width() : mySplit->height();
397   int need_pos = int( position * splitter_size );
398   int splitter_pos = 0;
399
400   if ( setPosition( wid, mySplit, o, need_pos, splitter_pos ) != 0 )
401   {
402     // impossible to set required position
403   }
404 }
405
406 /*!
407 * \brief Sets the action's accelerator key to accel. 
408 * \param id - the key of the action in the actions map.
409 * \param accel - action's accelerator key.
410 */
411 void QtxWorkstack::setAccel( const int id, const int accel )
412 {
413   if ( !myActionsMap.contains( id ) )
414     return;
415
416   myActionsMap[id]->setAccel( accel );
417 }
418
419 /*!
420 * \brief Returns the action's accelerator key.
421 * \param id - the key of the action in the actions map.
422 * \retval int  - action's accelerator key.
423 */
424 int QtxWorkstack::accel( const int id ) const
425 {
426   int res = 0;
427   if ( myActionsMap.contains( id ) )
428     res = myActionsMap[id]->accel();
429   return res;
430 }
431
432 static int positionSimple (QIntList& szList, const int nb, const int splitter_size,
433                            const int item_ind, const int item_rel_pos,
434                            const int need_pos, const int splitter_pos)
435 {
436   if (item_ind == 0) { // cannot move in this splitter
437     return (need_pos - splitter_pos);
438   }
439
440   int delta = 0;
441   int new_prev = 0;
442   int new_this = szList[item_ind];
443   int new_next = 0;
444
445   bool isToCheck = false;
446
447   if (need_pos < splitter_pos) {
448     // Set size of all previous workareas to zero <--
449     if (item_ind == nb - 1) {
450       // item iz last in the splitter, it will occupy all the splitter
451       new_this = splitter_size;
452     } else {
453       // recompute size of next items in splitter
454       new_next = (splitter_size - new_this) / (nb - item_ind - 1);
455     }
456     delta = need_pos - splitter_pos;
457
458   } else if (need_pos > (splitter_pos + splitter_size)) {
459     // Set size of all next workareas to zero -->
460     // recompute size of previous items in splitter
461     new_this = 0;
462     new_prev = (splitter_size - new_this) / item_ind;
463     delta = need_pos - (splitter_pos + splitter_size - new_this);
464
465   } else { // required position lays inside this splitter
466     // Move workarea inside splitter into required position <->
467     int new_item_rel_pos = need_pos - splitter_pos;
468     new_prev = new_item_rel_pos / item_ind;
469     if (need_pos < (splitter_pos + item_rel_pos)) {
470       // Make previous workareas smaller, next - bigger
471       // No problem to keep old size of the widget
472     } else {
473       // Make previous workareas bigger, next - smaller
474       if (new_this > splitter_size - new_item_rel_pos) {
475         new_this = splitter_size - new_item_rel_pos;
476       }
477       // jfa to do: in this case fixed size of next widgets could prevent right resizing
478       isToCheck = true;
479     }
480     if (item_ind == nb - 1) {
481       new_this = splitter_size - new_item_rel_pos;
482     } else {
483       new_next = (splitter_size - new_item_rel_pos - new_this) / (nb - item_ind - 1);
484     }
485     delta = 0;
486   }
487
488   setSizes (szList, item_ind, new_prev, new_this, new_next);
489   return delta;
490 }
491
492 /*!
493 * \brief Set position of given widget.
494 * \param wid          - widget to be moved
495 * \param split        - currently processed splitter (goes from more common
496 *                       to more particular splitter in recursion calls)
497 * \param o            - orientation of positioning
498 * \param need_pos     - required position of the given widget in pixels
499 *                       (from top/left side of workstack area)
500 * \param splitter_pos - position of the splitter \a split
501 *                       (from top/left side of workstack area)
502 * \retval int - returns difference between a required and a distinguished position.
503
504 * Internal method. Recursively calls itself.
505 * Is called from <VAR>SetRelativePosition</VAR> public method.
506 */
507 int QtxWorkstack::setPosition( QWidget* wid, QSplitter* split, const Qt::Orientation o,
508                                const int need_pos, const int splitter_pos )
509 {
510   if ( !wid || !split )
511     return need_pos - splitter_pos;
512
513   // Find corresponding sub-splitter.
514   // Find also index of appropriate item in current splitter.
515   int cur_ind = 0, item_ind = 0;
516   bool isBottom = false, isFound = false;
517   QSplitter* sub_split = NULL;
518   const QObjectList* objs = split->children();
519   if ( objs )
520   {
521     for (QObjectListIt it (*objs); it.current() && !isFound; ++it)
522     {
523       if (it.current()->inherits( "QtxWorkstackArea")) {
524         if (((QtxWorkstackArea*)it.current())->contains(wid)) {
525           item_ind = cur_ind;
526           isBottom = true;
527           isFound = true;
528         }
529         cur_ind++;
530       } else if (it.current()->inherits("QSplitter")) {
531         QPtrList<QtxWorkstackArea> areaList;
532         areas((QSplitter*)it.current(), areaList, true);
533         for (QPtrListIterator<QtxWorkstackArea> ita (areaList);
534              ita.current() && !isFound;
535              ++ita)
536         {
537           if (ita.current()->contains(wid)) {
538             item_ind = cur_ind;
539             isFound = true;
540             sub_split = (QSplitter*)it.current();
541           }
542         }
543         cur_ind++;
544       }
545     }
546   }
547   if (!isFound)
548     return (need_pos - splitter_pos);
549
550   if (split->orientation() == o) {
551     // Find coordinates of near and far sides of the appropriate item relatively current splitter
552     int splitter_size = (o == Horizontal ? split->width() : split->height());
553     QIntList szList = split->sizes();
554     int nb = szList.count();
555     int item_rel_pos = 0; // position of near side of item relatively this splitter
556     for (int i = 0; i < item_ind; i++) {
557       item_rel_pos += szList[i];
558     }
559     int item_size = szList[item_ind]; // size of item
560     int item_pos = splitter_pos + item_rel_pos;
561
562     // Resize splitter items to complete the conditions
563     if (isBottom) {
564       // I. Bottom of splitters stack reached
565
566       int delta = positionSimple(szList, nb, splitter_size, item_ind, item_rel_pos, need_pos, splitter_pos);
567       split->setSizes(szList);
568       // Recompute delta, as some windows can reject given size
569       int new_item_rel_pos = 0;
570       QIntList szList1 = split->sizes();
571       for (int i = 0; i < item_ind; i++) {
572         new_item_rel_pos += szList1[i];
573       }
574       delta = need_pos - (splitter_pos + new_item_rel_pos);
575       return delta;
576
577     } else {
578       // II. Bottom of splitters stack is not yet reached
579
580       if (item_ind == 0) { // cannot move in this splitter
581         // Process in sub-splitter
582         return setPosition(wid, sub_split, o, need_pos, splitter_pos);
583       }
584
585       int new_prev = 0;
586       int new_this = szList[item_ind];
587       int new_next = 0;
588
589       if (need_pos < splitter_pos) {
590         // Set size of all previous workareas to zero <--
591         if (item_ind == nb - 1) {
592           new_this = splitter_size;
593         } else {
594           new_next = (splitter_size - new_this) / (nb - item_ind - 1);
595         }
596         setSizes (szList, item_ind, new_prev, new_this, new_next);
597         split->setSizes(szList);
598         // Recompute splitter_pos, as some windows can reject given size
599         int new_item_rel_pos = 0;
600         QIntList szList1 = split->sizes();
601         for (int i = 0; i < item_ind; i++) {
602           new_item_rel_pos += szList1[i];
603         }
604         // Process in sub-splitter
605         return setPosition(wid, sub_split, o, need_pos, splitter_pos + new_item_rel_pos);
606       } else if (need_pos > (splitter_pos + splitter_size)) {
607         // Set size of all next workareas to zero -->
608         new_prev = (splitter_size - new_this) / item_ind;
609         setSizes (szList, item_ind, new_prev, new_this, new_next);
610         split->setSizes(szList);
611         // Recompute splitter_pos, as some windows can reject given size
612         int new_item_rel_pos = 0;
613         QIntList szList1 = split->sizes();
614         for (int i = 0; i < item_ind; i++) {
615           new_item_rel_pos += szList1[i];
616         }
617         // Process in sub-splitter
618         return setPosition(wid, sub_split, o, need_pos, splitter_pos + new_item_rel_pos);
619       } else {
620         // Set appropriate size of all previous/next items <->
621         int new_item_rel_pos = item_rel_pos;
622         if (need_pos < item_pos || (item_pos + item_size) < need_pos) {
623           // Move item inside splitter into required position <->
624           int new_this = szList[item_ind];
625           int new_next = 0;
626           new_item_rel_pos = need_pos - splitter_pos;
627           if ((item_pos + item_size) < need_pos) {
628             //new_item_rel_pos = need_pos - (item_pos + item_size);
629             new_item_rel_pos = item_rel_pos + (need_pos - (item_pos + item_size));
630           }
631           int new_prev = new_item_rel_pos / item_ind;
632           if (need_pos < (splitter_pos + item_rel_pos)) {
633             // Make previous workareas smaller, next - bigger
634             // No problem to keep old size of the widget
635           } else {
636             // Make previous workareas bigger, next - smaller
637             if (new_this > splitter_size - new_item_rel_pos) {
638               new_this = splitter_size - new_item_rel_pos;
639             }
640           }
641           if (item_ind == nb - 1) {
642             new_this = splitter_size - new_item_rel_pos;
643           } else {
644             new_next = (splitter_size - new_item_rel_pos - new_this) / (nb - item_ind - 1);
645           }
646           setSizes (szList, item_ind, new_prev, new_this, new_next);
647           split->setSizes(szList);
648           // Recompute new_item_rel_pos, as some windows can reject given size
649           new_item_rel_pos = 0;
650           QIntList szList1 = split->sizes();
651           for (int i = 0; i < item_ind; i++) {
652             new_item_rel_pos += szList1[i];
653           }
654         } else {
655           // Do nothing
656         }
657         // Process in sub-splitter
658         int add_pos = setPosition(wid, sub_split, o, need_pos, splitter_pos + new_item_rel_pos);
659         if (add_pos == 0)
660           return 0;
661
662         // this can be if corresponding workarea is first in sub-splitter
663         // or sub-splitter has another orientation
664
665         // Resize ones again to reach precize position <->
666         int need_pos_1 = splitter_pos + new_item_rel_pos + add_pos;
667
668         // Move workarea inside splitter into required position <->
669         int delta_1 = positionSimple(szList, nb, splitter_size, item_ind,
670                                      new_item_rel_pos, need_pos_1, splitter_pos);
671         split->setSizes(szList);
672         // Recompute new_item_rel_pos, as some windows can reject given size
673         new_item_rel_pos = 0;
674         QIntList szList1 = split->sizes();
675         for (int i = 0; i < item_ind; i++) {
676           new_item_rel_pos += szList1[i];
677         }
678         delta_1 = need_pos_1 - (splitter_pos + new_item_rel_pos);
679         return delta_1;
680       }
681     }
682   } else {
683     return setPosition(wid, sub_split, o, need_pos, splitter_pos);
684   }
685
686   return 0;
687 }
688
689 void QtxWorkstack::distributeSpace( QSplitter* split ) const
690 {
691   if ( !split )
692     return;
693
694   QIntList szList = split->sizes();
695   int size = ( split->orientation() == Horizontal ?
696                split->width() : split->height() ) / szList.count();
697   for ( QIntList::iterator it = szList.begin(); it != szList.end(); ++it )
698     *it = size;
699   split->setSizes( szList );
700 }
701
702 void QtxWorkstack::splitVertical()
703 {
704   split( Qt::Horizontal );
705 }
706
707 void QtxWorkstack::splitHorizontal()
708 {
709   split( Qt::Vertical );
710 }
711
712 void QtxWorkstack::onRename()
713 {
714   if ( !myWorkWin )
715     return;
716
717   bool ok = false;
718   QString newName = QInputDialog::getText( tr( "Rename" ), tr( "Enter new name:" ), QLineEdit::Normal,
719                                            myWorkWin->caption(), &ok, topLevelWidget() );
720   if ( ok && !newName.isEmpty() )
721     myWorkWin->setCaption( newName );
722 }
723
724 QSplitter* QtxWorkstack::wrapSplitter( QtxWorkstackArea* area )
725 {
726   if ( !area )
727     return 0;
728
729   QSplitter* pSplit = splitter( area );
730   if ( !pSplit )
731     return 0;
732
733   bool upd = pSplit->isUpdatesEnabled();
734   pSplit->setUpdatesEnabled( false );
735
736   QIntList szList = pSplit->sizes();
737
738   QSplitter* wrap = new QSplitter( 0 );
739 #if defined QT_VERSION && QT_VERSION >= 0x30200
740   wrap->setChildrenCollapsible( false );
741 #endif
742   insertWidget( wrap, pSplit, area );
743   area->reparent( wrap, QPoint( 0, 0 ), true );
744
745   pSplit->setSizes( szList );
746
747   pSplit->setUpdatesEnabled( upd );
748
749   return wrap;
750 }
751
752 void QtxWorkstack::insertWidget( QWidget* wid, QWidget* pWid, QWidget* after )
753 {
754   if ( !wid || !pWid )
755     return;
756
757   QWidgetList moveList;
758   const QObjectList* lst = pWid->children();
759   if ( lst )
760   {
761     bool found = false;
762     for ( QObjectListIt it( *lst ); it.current(); ++it )
763     {
764       if ( found && ( it.current()->inherits( "QSplitter" ) ||
765                       it.current()->inherits( "QtxWorkstackArea" ) ) )
766         moveList.append( (QWidget*)it.current() );
767       if ( it.current() == after )
768         found = true;
769     }
770   }
771
772   QMap<QWidget*, bool> map;
773   for ( QWidgetListIt it( moveList ); it.current(); ++it )
774   {
775     map.insert( it.current(), it.current()->isVisibleTo( it.current()->parentWidget() ) );
776     it.current()->reparent( 0, QPoint( 0, 0 ), false );
777   }
778
779   wid->reparent( pWid, QPoint( 0, 0 ), true );
780
781   for ( QWidgetListIt itr( moveList ); itr.current(); ++itr )
782     itr.current()->reparent( pWid, QPoint( 0, 0 ), map.contains( itr.current() ) ? map[itr.current()] : false );
783 }
784
785 /*!
786 * \brief Closes the active window.
787 */
788 void QtxWorkstack::onCloseWindow()
789 {
790   if ( myWorkWin )
791     myWorkWin->close();
792 }
793
794 void QtxWorkstack::onDestroyed( QObject* obj )
795 {
796   QtxWorkstackArea* area = (QtxWorkstackArea*)obj;
797
798   if ( area == myArea )
799     myArea = 0;
800
801   if ( !myArea )
802   {
803     QtxWorkstackArea* cur = neighbourArea( area );
804     if ( cur )
805       cur->setFocus();
806   }
807
808   QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
809 }
810
811 void QtxWorkstack::onWindowActivated( QWidget* wid )
812 {
813   const QObject* obj = sender();
814   if ( !obj->inherits( "QtxWorkstackArea" ) )
815     return;
816
817   setActiveArea( (QtxWorkstackArea*)obj );
818 }
819
820 void QtxWorkstack::onDeactivated( QtxWorkstackArea* area )
821 {
822   if ( myArea != area )
823     return;
824
825   QPtrList<QtxWorkstackArea> lst;
826   areas( mySplit, lst, true );
827
828   int idx = lst.find( area );
829   if ( idx == -1 )
830     return;
831
832   myWin = 0;
833   myArea = 0;
834
835   QtxWorkstackArea* newArea = neighbourArea( area );
836   if ( newArea && newArea->activeWidget() )
837     newArea->activeWidget()->setFocus();
838
839   QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
840 }
841
842 void QtxWorkstack::onContextMenuRequested( QWidget* w, QPoint p )
843 {
844   if ( !activeArea() )
845     return;
846
847   QWidgetList lst = activeArea()->widgetList();
848   if ( lst.isEmpty() )
849     return;
850
851   myWorkWin = w;
852
853   QPopupMenu* pm = new QPopupMenu();
854   
855   if ( lst.count() > 1 )
856   {
857     myActionsMap[SplitVertical]->addTo( pm );
858     myActionsMap[SplitHorizontal]->addTo( pm );
859     pm->insertSeparator();
860   }
861
862   if ( w )
863   {
864     myActionsMap[Close]->addTo( pm );
865     myActionsMap[Rename]->addTo( pm );
866   }
867
868   if ( pm->count() )
869     pm->exec( p );
870
871   delete pm;
872
873   myWorkWin = 0;
874 }
875
876 void QtxWorkstack::childEvent( QChildEvent* e )
877 {
878   if ( e->inserted() && e->child()->isWidgetType() )
879   {
880           QWidget* w = (QWidget*)e->child();
881           if ( w && w != mySplit )
882     {
883       targetArea()->insertWidget( w );
884       return;
885     }
886   }
887   QWidget::childEvent( e );
888 }
889
890 void QtxWorkstack::customEvent( QCustomEvent* e )
891 {
892   updateState();
893 }
894
895 QSplitter* QtxWorkstack::splitter( QtxWorkstackArea* area ) const
896 {
897   if ( !area )
898     return 0;
899
900   QSplitter* split = 0;
901
902   QWidget* wid = area->parentWidget();
903   if ( wid && wid->inherits( "QSplitter" ) )
904     split = (QSplitter*)wid;
905
906   return split;
907 }
908
909 void QtxWorkstack::splitters( QSplitter* split, QPtrList<QSplitter>& splitList, const bool rec ) const
910 {
911   if ( !split )
912     return;
913
914   const QObjectList* objs = split->children();
915   if ( objs )
916   {
917     for ( QObjectListIt it( *objs ); it.current(); ++it )
918     {
919       if ( rec )
920         splitters( (QSplitter*)it.current(), splitList, rec );
921       if ( it.current()->inherits( "QSplitter" ) )
922         splitList.append( (QSplitter*)it.current() );
923     }
924   }
925 }
926
927 void QtxWorkstack::areas( QSplitter* split, QPtrList<QtxWorkstackArea>& areaList, const bool rec ) const
928 {
929   if ( !split )
930     return;
931
932   const QObjectList* objs = split->children();
933   if ( objs )
934   {
935     for ( QObjectListIt it( *objs ); it.current(); ++it )
936     {
937       if ( it.current()->inherits( "QtxWorkstackArea" ) )
938         areaList.append( (QtxWorkstackArea*)it.current() );
939       else if ( rec && it.current()->inherits( "QSplitter" ) )
940         areas( (QSplitter*)it.current(), areaList, rec );
941     }
942   }
943 }
944
945 QtxWorkstackArea* QtxWorkstack::activeArea() const
946 {
947   return myArea;
948 }
949
950 QtxWorkstackArea* QtxWorkstack::targetArea()
951 {
952   QtxWorkstackArea* area = activeArea();
953   if ( !area )
954     area = currentArea();
955   if ( !area )
956   {
957     QPtrList<QtxWorkstackArea> lst;
958     areas( mySplit, lst );
959     if ( !lst.isEmpty() )
960       area = lst.first();
961   }
962
963   if ( !area )
964     area = createArea( mySplit );
965
966   return area;
967 }
968
969 QtxWorkstackArea* QtxWorkstack::currentArea() const
970 {
971   QtxWorkstackArea* area = 0;
972   QWidget* wid = focusWidget();
973   while ( wid && !area )
974   {
975     if ( wid->inherits( "QtxWorkstackArea" ) )
976       area = (QtxWorkstackArea*)wid;
977     wid = wid->parentWidget();
978   }
979
980   return area;
981 }
982
983 QtxWorkstackArea* QtxWorkstack::createArea( QWidget* parent ) const
984 {
985   QtxWorkstackArea* area = new QtxWorkstackArea( parent );
986
987   connect( area, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
988   connect( area, SIGNAL( activated( QWidget* ) ), this, SLOT( onWindowActivated( QWidget* ) ) );
989   connect( area, SIGNAL( contextMenuRequested( QWidget*, QPoint ) ),
990            this, SLOT( onContextMenuRequested( QWidget*, QPoint ) ) );
991   connect( area, SIGNAL( deactivated( QtxWorkstackArea* ) ), this, SLOT( onDeactivated( QtxWorkstackArea* ) ) );
992
993   return area;
994 }
995
996 void QtxWorkstack::setActiveArea( QtxWorkstackArea* area )
997 {
998   QWidget* oldCur = myWin;
999
1000   QtxWorkstackArea* oldArea = myArea;
1001
1002   myArea = area;
1003
1004   if ( myArea != oldArea )
1005   {
1006     if ( oldArea )
1007       oldArea->updateActiveState();
1008     if ( myArea )
1009       myArea->updateActiveState();
1010   }
1011
1012   if ( myArea )
1013     myWin = myArea->activeWidget();
1014
1015   if ( myWin && oldCur != myWin )
1016     emit windowActivated( myWin );
1017 }
1018
1019 QtxWorkstackArea* QtxWorkstack::neighbourArea( QtxWorkstackArea* area ) const
1020 {
1021   QPtrList<QtxWorkstackArea> lst;
1022   areas( mySplit, lst, true );
1023   int pos = lst.find( area );
1024   if ( pos < 0 )
1025     return 0;
1026
1027   QtxWorkstackArea* na = 0;
1028   for ( int i = pos - 1; i >= 0 && !na; i-- )
1029   {
1030     if ( !lst.at( i )->isEmpty() )
1031       na = lst.at( i );
1032   }
1033
1034   for ( int j = pos + 1; j < (int)lst.count() && !na; j++ )
1035   {
1036     if ( !lst.at( j )->isEmpty() )
1037         na = lst.at( j );
1038   }
1039   return na;
1040 }
1041
1042 QtxWorkstackArea* QtxWorkstack::areaAt( const QPoint& p ) const
1043 {
1044   QtxWorkstackArea* area = 0;
1045   QPtrList<QtxWorkstackArea> lst;
1046   areas( mySplit, lst, true );
1047   for ( QPtrListIterator<QtxWorkstackArea> it( lst ); it.current() && !area; ++it )
1048   {
1049     QtxWorkstackArea* cur = it.current();
1050     QRect r = cur->geometry();
1051     if ( cur->parentWidget() )
1052       r = QRect( cur->parentWidget()->mapToGlobal( r.topLeft() ), r.size() );
1053     if ( r.contains( p ) )
1054       area = cur;
1055   }
1056   return area;
1057 }
1058
1059 void QtxWorkstack::updateState()
1060 {
1061   updateState( mySplit );
1062 }
1063
1064 void QtxWorkstack::updateState( QSplitter* split )
1065 {
1066   QPtrList<QSplitter> recList;
1067   splitters( split, recList, false );
1068   for ( QPtrListIterator<QSplitter> itr( recList ); itr.current(); ++itr )
1069     updateState( itr.current() );
1070
1071   QPtrList<QSplitter> splitList;
1072   splitters( split, splitList, false );
1073
1074   QPtrList<QtxWorkstackArea> areaList;
1075   areas( split, areaList, false );
1076
1077   bool vis = false;
1078   for ( QPtrListIterator<QtxWorkstackArea> it( areaList ); it.current(); ++it )
1079   {
1080     if ( it.current()->isEmpty() )
1081       it.current()->hide();
1082     else
1083     {
1084       it.current()->show();
1085       vis = true;
1086     }
1087   }
1088
1089   if ( split == mySplit )
1090     return;
1091
1092   for ( QPtrListIterator<QSplitter> iter( splitList ); iter.current() && !vis; ++iter )
1093     vis = iter.current()->isVisibleTo( iter.current()->parentWidget() );
1094
1095   if ( areaList.isEmpty() && splitList.isEmpty() )
1096     delete split;
1097   else if ( vis )
1098     split->show();
1099   else
1100     split->hide();
1101 }
1102
1103 /*!
1104     Class: QtxWorkstackArea [Internal]
1105     Descr:
1106 */
1107
1108 QtxWorkstackArea::QtxWorkstackArea( QWidget* parent )
1109 : QWidget( parent )
1110 {
1111   QVBoxLayout* base = new QVBoxLayout( this );
1112
1113   QHBox* top = new QHBox( this );
1114   base->addWidget( top );
1115
1116   myBar = new QtxWorkstackTabBar( top );
1117
1118   QPushButton* close = new QPushButton( top );
1119   close->setPixmap( style().stylePixmap( QStyle::SP_TitleBarCloseButton ) );
1120   close->setAutoDefault( true );
1121   close->setFlat( true );
1122   myClose = close;
1123
1124   top->setStretchFactor( myBar, 1 );
1125
1126   myStack = new QWidgetStack( this );
1127
1128   base->addWidget( myStack, 1 );
1129
1130   connect( myClose, SIGNAL( clicked() ), this, SLOT( onClose() ) );
1131   connect( myBar, SIGNAL( selected( int ) ), this, SLOT( onSelected( int ) ) );
1132   connect( myBar, SIGNAL( dragActiveTab() ), this, SLOT( onDragActiveTab() ) );
1133   connect( myBar, SIGNAL( contextMenuRequested( QPoint ) ), this, SLOT( onContextMenuRequested( QPoint ) ) );
1134
1135   updateState();
1136
1137   updateActiveState();
1138
1139   qApp->installEventFilter( this );
1140 }
1141
1142 QtxWorkstackArea::~QtxWorkstackArea()
1143 {
1144   qApp->removeEventFilter( this );
1145 }
1146
1147 bool QtxWorkstackArea::isEmpty() const
1148 {
1149   bool res = false;
1150   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end() && !res; ++it )
1151     res = it.data().vis;
1152   return !res;
1153 }
1154
1155 void QtxWorkstackArea::insertWidget( QWidget* wid, const int idx )
1156 {
1157   if ( !wid )
1158     return;
1159
1160   int pos = myList.find( wid );
1161   if ( pos != -1 && ( pos == idx || ( idx < 0 && pos == (int)myList.count() - 1 ) ) )
1162     return;
1163
1164   myList.removeRef( wid );
1165   pos = idx < 0 ? myList.count() : idx;
1166   myList.insert( QMIN( pos, (int)myList.count() ), wid );
1167   if ( !myInfo.contains( wid ) )
1168   {
1169     QtxWorkstackChild* child = new QtxWorkstackChild( wid, myStack );
1170     myChild.insert( wid, child );
1171     myInfo.insert( wid, WidgetInfo() );
1172     myInfo[wid].id = generateId();
1173     myInfo[wid].vis = wid->isVisibleTo( wid->parentWidget() );
1174
1175     connect( child, SIGNAL( destroyed( QObject* ) ), this, SLOT( onChildDestroyed( QObject* ) ) );
1176     connect( wid, SIGNAL( destroyed() ), this, SLOT( onWidgetDestroyed() ) );
1177     connect( child, SIGNAL( shown( QtxWorkstackChild* ) ), this, SLOT( onChildShown( QtxWorkstackChild* ) ) );
1178     connect( child, SIGNAL( hided( QtxWorkstackChild* ) ), this, SLOT( onChildHided( QtxWorkstackChild* ) ) );
1179     connect( child, SIGNAL( activated( QtxWorkstackChild* ) ), this, SLOT( onChildActivated( QtxWorkstackChild* ) ) );
1180     connect( child, SIGNAL( captionChanged( QtxWorkstackChild* ) ), this, SLOT( onChildCaptionChanged( QtxWorkstackChild* ) ) );
1181   }
1182
1183   updateState();
1184
1185   setWidgetActive( wid );
1186 }
1187
1188 void QtxWorkstackArea::onContextMenuRequested( QPoint p )
1189 {
1190   const QtxWorkstackTabBar* bar = ::qt_cast<const QtxWorkstackTabBar*>( sender() );
1191   if ( !bar )
1192     return;
1193
1194   QWidget* wid = 0;
1195   QTab* tab = myBar->tabAt( tabAt( p ) );
1196   if ( tab )
1197     wid = widget( tab->identifier() );
1198
1199   emit contextMenuRequested( wid, p );
1200 }
1201
1202 void QtxWorkstackArea::onWidgetDestroyed()
1203 {
1204   if ( sender() )
1205     removeWidget( (QWidget*)sender(), false );
1206 }
1207
1208 void QtxWorkstackArea::removeWidget( QWidget* wid, const bool del )
1209 {
1210   if ( !myList.contains( wid ) )
1211     return;
1212
1213   if ( myBar->tab( widgetId( wid ) ) )
1214     myBar->removeTab( myBar->tab( widgetId( wid ) ) );
1215   myStack->removeWidget( child( wid ) );
1216
1217   myList.remove( wid );
1218   myInfo.remove( wid );
1219   myChild.remove( wid );
1220
1221   if( del )
1222   {
1223     delete child( wid );
1224     if( myList.isEmpty() )
1225       delete this;
1226     else
1227       updateState();
1228   }
1229   else
1230     updateState();
1231 }
1232
1233 QWidgetList QtxWorkstackArea::widgetList() const
1234 {
1235   QWidgetList lst;
1236   for ( QWidgetListIt it( myList ); it.current(); ++it )
1237   {
1238     if ( widgetVisibility( it.current() ) )
1239       lst.append( it.current() );
1240   }
1241   return lst;
1242 }
1243
1244 QWidget* QtxWorkstackArea::activeWidget() const
1245 {
1246   return widget( myBar->currentTab() );
1247 }
1248
1249 void QtxWorkstackArea::setActiveWidget( QWidget* wid )
1250 {
1251   myBar->setCurrentTab( widgetId( wid ) );
1252 }
1253
1254 bool QtxWorkstackArea::contains( QWidget* wid ) const
1255 {
1256   return myList.contains( wid );
1257 }
1258
1259 void QtxWorkstackArea::show()
1260 {
1261   QMap<QWidget*, bool> map;
1262   for ( QWidgetListIt it( myList ); it.current(); ++it )
1263   {
1264     map.insert( it.current(), isBlocked( it.current() ) );
1265     setBlocked( it.current(), true );
1266   }
1267
1268   QWidget::show();
1269
1270   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1271     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1272 }
1273
1274 void QtxWorkstackArea::hide()
1275 {
1276   QMap<QWidget*, bool> map;
1277   for ( QWidgetListIt it( myList ); it.current(); ++it )
1278   {
1279     map.insert( it.current(), isBlocked( it.current() ) );
1280     setBlocked( it.current(), true );
1281   }
1282
1283   QWidget::hide();
1284
1285   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1286     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1287 }
1288
1289 bool QtxWorkstackArea::isActive() const
1290 {
1291   QtxWorkstack* ws = workstack();
1292   if ( !ws )
1293     return false;
1294
1295   return ws->activeArea() == this;
1296 }
1297
1298 void QtxWorkstackArea::updateActiveState()
1299 {
1300   myBar->setActive( isActive() );
1301 }
1302
1303 QtxWorkstack* QtxWorkstackArea::workstack() const
1304 {
1305   QtxWorkstack* ws = 0;
1306   QWidget* wid = parentWidget();
1307   while ( wid && !ws )
1308   {
1309     if ( wid->inherits( "QtxWorkstack" ) )
1310       ws = (QtxWorkstack*)wid;
1311     wid = wid->parentWidget();
1312   }
1313   return ws;
1314 }
1315
1316 bool QtxWorkstackArea::eventFilter( QObject* o, QEvent* e )
1317 {
1318   if ( o->isWidgetType() )
1319   {
1320     QWidget* wid = (QWidget*)o;
1321     if ( e->type() == QEvent::FocusIn || e->type() == QEvent::MouseButtonPress )
1322     {
1323       bool ok = false;
1324       while ( !ok && wid && wid != myClose )
1325       {
1326         ok = wid == this;
1327         wid = wid->parentWidget();
1328       }
1329       if ( ok )
1330         QApplication::postEvent( this, new QCustomEvent( (QEvent::Type)( e->type() == QEvent::FocusIn ? ActivateWidget : FocusWidget ) ) );
1331     }
1332   }
1333   return false;
1334 }
1335
1336 QRect QtxWorkstackArea::floatRect() const
1337 {
1338   QRect r = myStack->geometry();
1339   return QRect( mapToGlobal( r.topLeft() ), mapToGlobal( r.bottomRight() ) );
1340 }
1341
1342 QRect QtxWorkstackArea::floatTab( const int idx ) const
1343 {
1344   return myBar->tabRect( idx );
1345 }
1346
1347 int QtxWorkstackArea::tabAt( const QPoint& p ) const
1348 {
1349   int idx = -1;
1350   for ( int i = 0; i < myBar->count() && idx == -1; i++ )
1351   {
1352     QRect r = myBar->tabRect( i );
1353     if ( r.isValid() && r.contains( p ) )
1354       idx = i;
1355   }
1356   return idx;
1357 }
1358
1359 void QtxWorkstackArea::customEvent( QCustomEvent* e )
1360 {
1361   switch ( e->type() )
1362   {
1363   case ActivateWidget:
1364     emit activated( activeWidget() );
1365     break;
1366   case FocusWidget:
1367     if ( activeWidget() && !activeWidget()->focusWidget() )
1368       activeWidget()->setFocus();
1369     break;
1370   case RemoveWidget:
1371     removeWidget( (QWidget*)e->data() );
1372     break;
1373   }
1374 }
1375
1376 void QtxWorkstackArea::focusInEvent( QFocusEvent* e )
1377 {
1378   QWidget::focusInEvent( e );
1379
1380   emit activated( activeWidget() );
1381 }
1382
1383 void QtxWorkstackArea::mousePressEvent( QMouseEvent* e )
1384 {
1385   QWidget::mousePressEvent( e );
1386
1387   emit activated( activeWidget() );
1388 }
1389
1390 void QtxWorkstackArea::onClose()
1391 {
1392   QWidget* wid = activeWidget();
1393   if ( wid )
1394     wid->close();
1395 }
1396
1397 void QtxWorkstackArea::onSelected( int id )
1398 {
1399   updateCurrent();
1400
1401   emit activated( activeWidget() );
1402 }
1403
1404 void QtxWorkstackArea::onDragActiveTab()
1405 {
1406   QtxWorkstackChild* c = child( activeWidget() );
1407   if ( !c )
1408     return;
1409
1410   new QtxWorkstackDrag( workstack(), c );
1411 }
1412
1413 void QtxWorkstackArea::onChildDestroyed( QObject* obj )
1414 {
1415   QtxWorkstackChild* child = (QtxWorkstackChild*)obj;
1416   myStack->removeWidget( child );
1417
1418   QWidget* wid = 0;
1419   for ( ChildMap::ConstIterator it = myChild.begin(); it != myChild.end() && !wid; ++it )
1420   {
1421     if ( it.data() == child )
1422       wid = it.key();
1423   }
1424
1425   myChild.remove( wid );
1426
1427   QApplication::postEvent( this, new QCustomEvent( (QEvent::Type)RemoveWidget, wid ) );
1428 }
1429
1430 void QtxWorkstackArea::onChildShown( QtxWorkstackChild* c )
1431 {
1432   setWidgetShown( c->widget(), true );
1433 }
1434
1435 void QtxWorkstackArea::onChildHided( QtxWorkstackChild* c )
1436 {
1437   setWidgetShown( c->widget(), false );
1438 }
1439
1440 void QtxWorkstackArea::onChildActivated( QtxWorkstackChild* c )
1441 {
1442   setWidgetActive( c->widget() );
1443 }
1444
1445 void QtxWorkstackArea::onChildCaptionChanged( QtxWorkstackChild* c )
1446 {
1447   updateTab( c->widget() );
1448 }
1449
1450 void QtxWorkstackArea::updateCurrent()
1451 {
1452   QMap<QWidget*, bool> map;
1453   for ( QWidgetListIt it( myList ); it.current(); ++it )
1454   {
1455     map.insert( it.current(), isBlocked( it.current() ) );
1456     setBlocked( it.current(), true );
1457   }
1458
1459   myStack->raiseWidget( myBar->currentTab() );
1460
1461   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1462     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1463 }
1464
1465 void QtxWorkstackArea::updateTab( QWidget* wid )
1466 {
1467   QTab* tab = myBar->tab( widgetId( wid ) );
1468   if ( !tab )
1469     return;
1470
1471   QIconSet icoSet;
1472   if ( wid->icon() )
1473   {
1474     QPixmap pix = *wid->icon();
1475     pix.convertFromImage( pix.convertToImage().smoothScale( pix.width(), 16, QImage::ScaleMin ) );
1476     icoSet = QIconSet( pix );
1477   }
1478
1479   tab->setIconSet( icoSet );
1480   tab->setText( wid->caption() );
1481 }
1482
1483 QWidget* QtxWorkstackArea::widget( const int id ) const
1484 {
1485   QWidget* wid = 0;
1486   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end() && !wid; ++it )
1487   {
1488     if ( it.data().id == id )
1489       wid = it.key();
1490   }
1491   return wid;
1492 }
1493
1494 int QtxWorkstackArea::widgetId( QWidget* wid ) const
1495 {
1496   int id = -1;
1497   if ( myInfo.contains( wid ) )
1498     id = myInfo[wid].id;
1499   return id;
1500 }
1501
1502 bool QtxWorkstackArea::widgetVisibility( QWidget* wid ) const
1503 {
1504   bool res = false;
1505   if ( myInfo.contains( wid ) )
1506     res = myInfo[wid].vis;
1507   return res;
1508 }
1509
1510 void QtxWorkstackArea::setWidgetActive( QWidget* wid )
1511 {
1512   int id = widgetId( wid );
1513   if ( id < 0 )
1514     return;
1515
1516   myBar->setCurrentTab( id );
1517 }
1518
1519 void QtxWorkstackArea::setWidgetShown( QWidget* wid, const bool on )
1520 {
1521   if ( isBlocked( wid ) || !myInfo.contains( wid ) || myInfo[wid].vis == on )
1522     return;
1523
1524   myInfo[wid].vis = on;
1525   updateState();
1526 }
1527
1528 void QtxWorkstackArea::updateState()
1529 {
1530   bool updBar = myBar->isUpdatesEnabled();
1531   bool updStk = myStack->isUpdatesEnabled();
1532   myBar->setUpdatesEnabled( false );
1533   myStack->setUpdatesEnabled( false );
1534
1535   bool block = myBar->signalsBlocked();
1536   myBar->blockSignals( true );
1537
1538   QWidget* prev = activeWidget();
1539
1540   int idx = 0;
1541   for ( QWidgetListIt it( myList ); it.current(); ++it )
1542   {
1543     QWidget* wid = it.current();
1544     int id = widgetId( wid );
1545
1546     if ( id < 0 )
1547       continue;
1548
1549     bool vis = widgetVisibility( wid );
1550
1551     if ( myBar->tab( id ) && ( !vis || myBar->indexOf( id ) != idx ) )
1552       myBar->removeTab( myBar->tab( id ) );
1553
1554     if ( !myBar->tab( id ) && vis )
1555     {
1556       QTab* tab = new QTab( wid->caption() );
1557       myBar->insertTab( tab, idx );
1558       tab->setIdentifier( id );
1559     }
1560
1561     updateTab( wid );
1562
1563     bool block = isBlocked( wid );
1564     setBlocked( wid, true );
1565
1566     QtxWorkstackChild* cont = child( wid );
1567
1568     if ( !vis )
1569       myStack->removeWidget( cont );
1570     else if ( !myStack->widget( id ) )
1571       myStack->addWidget( cont, id );
1572
1573     if ( vis )
1574       idx++;
1575
1576     setBlocked( wid, block );
1577   }
1578
1579   int curId = widgetId( prev );
1580   if ( !myBar->tab( curId ) )
1581   {
1582     QWidget* wid = 0;
1583     int pos = myList.find( prev );
1584     for ( int i = pos - 1; i >= 0 && !wid; i-- )
1585     {
1586       if ( widgetVisibility( myList.at( i ) ) )
1587         wid = myList.at( i );
1588     }
1589
1590     for ( int j = pos + 1; j < (int)myList.count() && !wid; j++ )
1591     {
1592       if ( widgetVisibility( myList.at( j ) ) )
1593         wid = myList.at( j );
1594     }
1595
1596     if ( wid )
1597       curId = widgetId( wid );
1598   }
1599
1600   myBar->setCurrentTab( curId );
1601
1602   myBar->blockSignals( block );
1603
1604   updateCurrent();
1605
1606   myBar->setUpdatesEnabled( updBar );
1607   myStack->setUpdatesEnabled( updStk );
1608   if ( updBar )
1609     myBar->update();
1610   if ( updStk )
1611     myStack->update();
1612
1613   QResizeEvent re( myBar->size(), myBar->size() );
1614   QApplication::sendEvent( myBar, &re );
1615
1616   if ( isEmpty() )
1617   {
1618     hide();
1619     emit deactivated( this );
1620   }
1621   else
1622   {
1623     show();
1624     if ( prev != activeWidget() )
1625       emit activated( activeWidget() );
1626   }
1627 }
1628
1629 int QtxWorkstackArea::generateId() const
1630 {
1631   QMap<int, int> map;
1632
1633   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end(); ++it )
1634     map.insert( it.data().id, 0 );
1635
1636   int id = 0;
1637   while ( map.contains( id ) )
1638     id++;
1639
1640   return id;
1641 }
1642
1643 bool QtxWorkstackArea::isBlocked( QWidget* wid ) const
1644 {
1645   return myBlock.contains( wid );
1646 }
1647
1648 void QtxWorkstackArea::setBlocked( QWidget* wid, const bool on )
1649 {
1650   if ( on )
1651     myBlock.insert( wid, 0 );
1652   else
1653     myBlock.remove( wid );
1654 }
1655
1656 QtxWorkstackChild* QtxWorkstackArea::child( QWidget* wid ) const
1657 {
1658   QtxWorkstackChild* res = 0;
1659   if ( myChild.contains( wid ) )
1660     res = myChild[wid];
1661   return res;
1662 }
1663
1664 /*!
1665     Class: QtxWorkstackChild [Internal]
1666     Descr:
1667 */
1668
1669 QtxWorkstackChild::QtxWorkstackChild( QWidget* wid, QWidget* parent )
1670 : QHBox( parent ),
1671 myWidget( wid )
1672 {
1673   myWidget->reparent( this, QPoint( 0, 0 ), myWidget->isVisibleTo( myWidget->parentWidget() ) );
1674   myWidget->installEventFilter( this );
1675
1676   connect( myWidget, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
1677 }
1678
1679 QtxWorkstackChild::~QtxWorkstackChild()
1680 {
1681   qApp->removeEventFilter( this );
1682
1683   if ( !widget() )
1684     return;
1685
1686   widget()->removeEventFilter( this );
1687   widget()->reparent( 0, QPoint( 0, 0 ), false );
1688   disconnect( widget(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
1689 }
1690
1691 QWidget* QtxWorkstackChild::widget() const
1692 {
1693   return myWidget;
1694 }
1695
1696 bool QtxWorkstackChild::eventFilter( QObject* o, QEvent* e )
1697 {
1698   if ( o->isWidgetType() )
1699   {
1700     if ( e->type() == QEvent::CaptionChange || e->type() == QEvent::IconChange )
1701       emit captionChanged( this );
1702
1703     if ( !e->spontaneous() && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) )
1704       emit shown( this );
1705
1706     if ( !e->spontaneous() && ( e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
1707       emit hided( this );
1708
1709     if ( e->type() == QEvent::FocusIn )
1710       emit activated( this );
1711   }
1712   return QHBox::eventFilter( o, e );
1713 }
1714
1715 void QtxWorkstackChild::onDestroyed( QObject* obj )
1716 {
1717   if ( obj != widget() )
1718     return;
1719
1720   myWidget = 0;
1721   deleteLater();
1722 }
1723
1724 void QtxWorkstackChild::childEvent( QChildEvent* e )
1725 {
1726   if ( e->type() == QEvent::ChildRemoved && e->child() == widget() )
1727   {
1728     myWidget = 0;
1729     deleteLater();
1730   }
1731   QHBox::childEvent( e );
1732 }
1733
1734 /*!
1735     Class: QtxWorkstackTabBar [Internal]
1736     Descr:
1737 */
1738
1739 QtxWorkstackTabBar::QtxWorkstackTabBar( QWidget* parent )
1740 : QTabBar( parent ),
1741 myId( -1 )
1742 {
1743 }
1744
1745 QtxWorkstackTabBar::~QtxWorkstackTabBar()
1746 {
1747 }
1748
1749 void QtxWorkstackTabBar::setActive( const bool on )
1750 {
1751   QFont aFont = font();
1752   aFont.setUnderline( on );
1753   setFont( aFont );
1754
1755   update();
1756 }
1757
1758 QRect QtxWorkstackTabBar::tabRect( const int idx ) const
1759 {
1760   QRect r;
1761   QTab* t = tabAt( idx );
1762   if ( t )
1763   {
1764     r = t->rect();
1765     r.setLeft( QMAX( r.left(), 0 ) );
1766
1767     int x1 = tabAt( 0 )->rect().left();
1768     int x2 = tabAt( count() - 1 )->rect().right();
1769
1770     int bw = 0;
1771     if ( QABS( x2 - x1 ) > width() )
1772 #if defined QT_VERSION && QT_VERSION >= 0x30300
1773       bw = 2 * style().pixelMetric( QStyle::PM_TabBarScrollButtonWidth, this );
1774 #else
1775       bw = 2 * 16;
1776 #endif
1777
1778     int limit = width() - bw;
1779     r.setRight( QMIN( r.right(), limit ) );
1780
1781     r = QRect( mapToGlobal( r.topLeft() ), r.size() );
1782   }
1783   return r;
1784 }
1785
1786 void QtxWorkstackTabBar::mouseMoveEvent( QMouseEvent* e )
1787 {
1788   if ( myId != -1 && !tab( myId )->rect().contains( e->pos() ) )
1789   {
1790     myId = -1;
1791     emit dragActiveTab();
1792   }
1793
1794   QTabBar::mouseMoveEvent( e );
1795 }
1796
1797 void QtxWorkstackTabBar::mousePressEvent( QMouseEvent* e )
1798 {
1799   QTabBar::mousePressEvent( e );
1800
1801   if ( e->button() == LeftButton )
1802     myId = currentTab();
1803 }
1804
1805 void QtxWorkstackTabBar::mouseReleaseEvent( QMouseEvent* e )
1806 {
1807   QTabBar::mouseReleaseEvent( e );
1808
1809   myId = -1;
1810
1811   if ( e->button() == RightButton )
1812     emit contextMenuRequested( e->globalPos() );
1813 }
1814
1815 void QtxWorkstackTabBar::contextMenuEvent( QContextMenuEvent* e )
1816 {
1817   if ( e->reason() != QContextMenuEvent::Mouse )
1818     emit contextMenuRequested( e->globalPos() );
1819 }
1820
1821 void QtxWorkstackTabBar::paintLabel( QPainter* p, const QRect& br, QTab* t, bool has_focus ) const
1822 {
1823   if ( currentTab() != t->identifier() )
1824   {
1825     QFont fnt = p->font();
1826     fnt.setUnderline( false );
1827     p->setFont( fnt );
1828   }
1829   QTabBar::paintLabel( p, br, t, has_focus );
1830 }
1831
1832 /*!
1833     Class: QtxWorkstackDrag [Internal]
1834     Descr:
1835 */
1836
1837 QtxWorkstackDrag::QtxWorkstackDrag( QtxWorkstack* ws, QtxWorkstackChild* child )
1838 : QObject( 0 ),
1839 myWS( ws ),
1840 myTab( -1 ),
1841 myArea( 0 ),
1842 myPainter( 0 ),
1843 myChild( child )
1844 {
1845   qApp->installEventFilter( this );
1846 }
1847
1848 QtxWorkstackDrag::~QtxWorkstackDrag()
1849 {
1850   qApp->removeEventFilter( this );
1851
1852   endDrawRect();
1853 }
1854
1855 bool QtxWorkstackDrag::eventFilter( QObject*, QEvent* e )
1856 {
1857   switch ( e->type() )
1858   {
1859   case QEvent::MouseMove:
1860     updateTarget( ((QMouseEvent*)e)->globalPos() );
1861     break;
1862   case QEvent::MouseButtonRelease:
1863     drawRect();
1864     endDrawRect();
1865     dropWidget();
1866     deleteLater();
1867     break;
1868   default:
1869     return false;
1870   }
1871   return true;
1872 }
1873
1874 void QtxWorkstackDrag::updateTarget( const QPoint& p )
1875 {
1876   int tab = -1;
1877   QtxWorkstackArea* area = detectTarget( p, tab );
1878   setTarget( area, tab );
1879 }
1880
1881 QtxWorkstackArea* QtxWorkstackDrag::detectTarget( const QPoint& p, int& tab ) const
1882 {
1883   if ( p.isNull() )
1884     return 0;
1885
1886   QtxWorkstackArea* area = myWS->areaAt( p );
1887   if ( area )
1888     tab = area->tabAt( p );
1889   return area;
1890 }
1891
1892 void QtxWorkstackDrag::setTarget( QtxWorkstackArea* area, const int tab )
1893 {
1894   if ( !area || ( myArea == area && tab == myTab ) )
1895     return;
1896
1897   startDrawRect();
1898
1899   if ( myArea )
1900     drawRect();
1901
1902   myTab = tab;
1903   myArea = area;
1904
1905   if ( myArea )
1906     drawRect();
1907 }
1908
1909 void QtxWorkstackDrag::dropWidget()
1910 {
1911   if ( myArea )
1912     myArea->insertWidget( myChild->widget(), myTab );
1913 }
1914
1915 void QtxWorkstackDrag::drawRect()
1916 {
1917   if ( !myPainter || !myArea )
1918     return;
1919
1920   QRect r = myArea->floatRect();
1921   int m = myPainter->pen().width();
1922
1923   r.setTop( r.top() + m + 2 );
1924   r.setLeft( r.left() + m + 2 );
1925   r.setRight( r.right() - m - 2 );
1926   r.setBottom( r.bottom() - m - 2 );
1927
1928   myPainter->drawRect( r );
1929
1930   QRect tr = myArea->floatTab( myTab );
1931   tr.setTop( tr.top() + m );
1932   tr.setLeft( tr.left() + m );
1933   tr.setRight( tr.right() - m );
1934   tr.setBottom( tr.bottom() - m );
1935
1936   myPainter->drawRect( tr );
1937 }
1938
1939 void QtxWorkstackDrag::endDrawRect()
1940 {
1941   delete myPainter;
1942   myPainter = 0;
1943 }
1944
1945 void QtxWorkstackDrag::startDrawRect()
1946 {
1947   if ( myPainter )
1948     return;
1949
1950   int scr = QApplication::desktop()->screenNumber( (QWidget*)this );
1951   QWidget* paint_on = QApplication::desktop()->screen( scr );
1952
1953   myPainter = new QPainter( paint_on, true );
1954   myPainter->setPen( QPen( gray, 3 ) );
1955   myPainter->setRasterOp( XorROP );
1956 }