Salome HOME
72275a837bdb3e4195a14230ee870cb5786e90dc
[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   else
793     activeWindow()->close();
794 }
795
796 void QtxWorkstack::onDestroyed( QObject* obj )
797 {
798   QtxWorkstackArea* area = (QtxWorkstackArea*)obj;
799
800   if ( area == myArea )
801     myArea = 0;
802
803   if ( !myArea )
804   {
805     QtxWorkstackArea* cur = neighbourArea( area );
806     if ( cur )
807       cur->setFocus();
808   }
809
810   QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
811 }
812
813 void QtxWorkstack::onWindowActivated( QWidget* wid )
814 {
815   const QObject* obj = sender();
816   if ( !obj->inherits( "QtxWorkstackArea" ) )
817     return;
818
819   setActiveArea( (QtxWorkstackArea*)obj );
820 }
821
822 void QtxWorkstack::onDeactivated( QtxWorkstackArea* area )
823 {
824   if ( myArea != area )
825     return;
826
827   QPtrList<QtxWorkstackArea> lst;
828   areas( mySplit, lst, true );
829
830   int idx = lst.find( area );
831   if ( idx == -1 )
832     return;
833
834   myWin = 0;
835   myArea = 0;
836
837   QtxWorkstackArea* newArea = neighbourArea( area );
838   if ( newArea && newArea->activeWidget() )
839     newArea->activeWidget()->setFocus();
840
841   QApplication::postEvent( this, new QCustomEvent( QEvent::User ) );
842 }
843
844 void QtxWorkstack::onContextMenuRequested( QWidget* w, QPoint p )
845 {
846   if ( !activeArea() )
847     return;
848
849   QWidgetList lst = activeArea()->widgetList();
850   if ( lst.isEmpty() )
851     return;
852
853   myWorkWin = w;
854
855   QPopupMenu* pm = new QPopupMenu();
856   
857   if ( lst.count() > 1 )
858   {
859     myActionsMap[SplitVertical]->addTo( pm );
860     myActionsMap[SplitHorizontal]->addTo( pm );
861     pm->insertSeparator();
862   }
863
864   if ( w )
865   {
866     myActionsMap[Close]->addTo( pm );
867     myActionsMap[Rename]->addTo( pm );
868   }
869
870   if ( pm->count() )
871     pm->exec( p );
872
873   delete pm;
874
875   myWorkWin = 0;
876 }
877
878 void QtxWorkstack::childEvent( QChildEvent* e )
879 {
880   if ( e->inserted() && e->child()->isWidgetType() )
881   {
882           QWidget* w = (QWidget*)e->child();
883           if ( w && w != mySplit )
884     {
885       targetArea()->insertWidget( w );
886       return;
887     }
888   }
889   QWidget::childEvent( e );
890 }
891
892 void QtxWorkstack::customEvent( QCustomEvent* e )
893 {
894   updateState();
895 }
896
897 QSplitter* QtxWorkstack::splitter( QtxWorkstackArea* area ) const
898 {
899   if ( !area )
900     return 0;
901
902   QSplitter* split = 0;
903
904   QWidget* wid = area->parentWidget();
905   if ( wid && wid->inherits( "QSplitter" ) )
906     split = (QSplitter*)wid;
907
908   return split;
909 }
910
911 void QtxWorkstack::splitters( QSplitter* split, QPtrList<QSplitter>& splitList, const bool rec ) const
912 {
913   if ( !split )
914     return;
915
916   const QObjectList* objs = split->children();
917   if ( objs )
918   {
919     for ( QObjectListIt it( *objs ); it.current(); ++it )
920     {
921       if ( rec )
922         splitters( (QSplitter*)it.current(), splitList, rec );
923       if ( it.current()->inherits( "QSplitter" ) )
924         splitList.append( (QSplitter*)it.current() );
925     }
926   }
927 }
928
929 void QtxWorkstack::areas( QSplitter* split, QPtrList<QtxWorkstackArea>& areaList, const bool rec ) const
930 {
931   if ( !split )
932     return;
933
934   const QObjectList* objs = split->children();
935   if ( objs )
936   {
937     for ( QObjectListIt it( *objs ); it.current(); ++it )
938     {
939       if ( it.current()->inherits( "QtxWorkstackArea" ) )
940         areaList.append( (QtxWorkstackArea*)it.current() );
941       else if ( rec && it.current()->inherits( "QSplitter" ) )
942         areas( (QSplitter*)it.current(), areaList, rec );
943     }
944   }
945 }
946
947 QtxWorkstackArea* QtxWorkstack::activeArea() const
948 {
949   return myArea;
950 }
951
952 QtxWorkstackArea* QtxWorkstack::targetArea()
953 {
954   QtxWorkstackArea* area = activeArea();
955   if ( !area )
956     area = currentArea();
957   if ( !area )
958   {
959     QPtrList<QtxWorkstackArea> lst;
960     areas( mySplit, lst );
961     if ( !lst.isEmpty() )
962       area = lst.first();
963   }
964
965   if ( !area )
966     area = createArea( mySplit );
967
968   return area;
969 }
970
971 QtxWorkstackArea* QtxWorkstack::currentArea() const
972 {
973   QtxWorkstackArea* area = 0;
974   QWidget* wid = focusWidget();
975   while ( wid && !area )
976   {
977     if ( wid->inherits( "QtxWorkstackArea" ) )
978       area = (QtxWorkstackArea*)wid;
979     wid = wid->parentWidget();
980   }
981
982   return area;
983 }
984
985 QtxWorkstackArea* QtxWorkstack::createArea( QWidget* parent ) const
986 {
987   QtxWorkstackArea* area = new QtxWorkstackArea( parent );
988
989   connect( area, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
990   connect( area, SIGNAL( activated( QWidget* ) ), this, SLOT( onWindowActivated( QWidget* ) ) );
991   connect( area, SIGNAL( contextMenuRequested( QWidget*, QPoint ) ),
992            this, SLOT( onContextMenuRequested( QWidget*, QPoint ) ) );
993   connect( area, SIGNAL( deactivated( QtxWorkstackArea* ) ), this, SLOT( onDeactivated( QtxWorkstackArea* ) ) );
994
995   return area;
996 }
997
998 void QtxWorkstack::setActiveArea( QtxWorkstackArea* area )
999 {
1000   QWidget* oldCur = myWin;
1001
1002   QtxWorkstackArea* oldArea = myArea;
1003
1004   myArea = area;
1005
1006   if ( myArea != oldArea )
1007   {
1008     if ( oldArea )
1009       oldArea->updateActiveState();
1010     if ( myArea )
1011       myArea->updateActiveState();
1012   }
1013
1014   if ( myArea )
1015     myWin = myArea->activeWidget();
1016
1017   if ( myWin && oldCur != myWin )
1018     emit windowActivated( myWin );
1019 }
1020
1021 QtxWorkstackArea* QtxWorkstack::neighbourArea( QtxWorkstackArea* area ) const
1022 {
1023   QPtrList<QtxWorkstackArea> lst;
1024   areas( mySplit, lst, true );
1025   int pos = lst.find( area );
1026   if ( pos < 0 )
1027     return 0;
1028
1029   QtxWorkstackArea* na = 0;
1030   for ( int i = pos - 1; i >= 0 && !na; i-- )
1031   {
1032     if ( !lst.at( i )->isEmpty() )
1033       na = lst.at( i );
1034   }
1035
1036   for ( int j = pos + 1; j < (int)lst.count() && !na; j++ )
1037   {
1038     if ( !lst.at( j )->isEmpty() )
1039         na = lst.at( j );
1040   }
1041   return na;
1042 }
1043
1044 QtxWorkstackArea* QtxWorkstack::areaAt( const QPoint& p ) const
1045 {
1046   QtxWorkstackArea* area = 0;
1047   QPtrList<QtxWorkstackArea> lst;
1048   areas( mySplit, lst, true );
1049   for ( QPtrListIterator<QtxWorkstackArea> it( lst ); it.current() && !area; ++it )
1050   {
1051     QtxWorkstackArea* cur = it.current();
1052     QRect r = cur->geometry();
1053     if ( cur->parentWidget() )
1054       r = QRect( cur->parentWidget()->mapToGlobal( r.topLeft() ), r.size() );
1055     if ( r.contains( p ) )
1056       area = cur;
1057   }
1058   return area;
1059 }
1060
1061 void QtxWorkstack::updateState()
1062 {
1063   updateState( mySplit );
1064 }
1065
1066 void QtxWorkstack::updateState( QSplitter* split )
1067 {
1068   QPtrList<QSplitter> recList;
1069   splitters( split, recList, false );
1070   for ( QPtrListIterator<QSplitter> itr( recList ); itr.current(); ++itr )
1071     updateState( itr.current() );
1072
1073   QPtrList<QSplitter> splitList;
1074   splitters( split, splitList, false );
1075
1076   QPtrList<QtxWorkstackArea> areaList;
1077   areas( split, areaList, false );
1078
1079   bool vis = false;
1080   for ( QPtrListIterator<QtxWorkstackArea> it( areaList ); it.current(); ++it )
1081   {
1082     if ( it.current()->isEmpty() )
1083       it.current()->hide();
1084     else
1085     {
1086       it.current()->show();
1087       vis = true;
1088     }
1089   }
1090
1091   if ( split == mySplit )
1092     return;
1093
1094   for ( QPtrListIterator<QSplitter> iter( splitList ); iter.current() && !vis; ++iter )
1095     vis = iter.current()->isVisibleTo( iter.current()->parentWidget() );
1096
1097   if ( areaList.isEmpty() && splitList.isEmpty() )
1098     delete split;
1099   else if ( vis )
1100     split->show();
1101   else
1102     split->hide();
1103 }
1104
1105 /*!
1106     Class: QtxWorkstackArea [Internal]
1107     Descr:
1108 */
1109
1110 QtxWorkstackArea::QtxWorkstackArea( QWidget* parent )
1111 : QWidget( parent )
1112 {
1113   QVBoxLayout* base = new QVBoxLayout( this );
1114
1115   QHBox* top = new QHBox( this );
1116   base->addWidget( top );
1117
1118   myBar = new QtxWorkstackTabBar( top );
1119
1120   QPushButton* close = new QPushButton( top );
1121   close->setPixmap( style().stylePixmap( QStyle::SP_TitleBarCloseButton ) );
1122   close->setAutoDefault( true );
1123   close->setFlat( true );
1124   myClose = close;
1125
1126   top->setStretchFactor( myBar, 1 );
1127
1128   myStack = new QWidgetStack( this );
1129
1130   base->addWidget( myStack, 1 );
1131
1132   connect( myClose, SIGNAL( clicked() ), this, SLOT( onClose() ) );
1133   connect( myBar, SIGNAL( selected( int ) ), this, SLOT( onSelected( int ) ) );
1134   connect( myBar, SIGNAL( dragActiveTab() ), this, SLOT( onDragActiveTab() ) );
1135   connect( myBar, SIGNAL( contextMenuRequested( QPoint ) ), this, SLOT( onContextMenuRequested( QPoint ) ) );
1136
1137   updateState();
1138
1139   updateActiveState();
1140
1141   qApp->installEventFilter( this );
1142 }
1143
1144 QtxWorkstackArea::~QtxWorkstackArea()
1145 {
1146   qApp->removeEventFilter( this );
1147 }
1148
1149 bool QtxWorkstackArea::isEmpty() const
1150 {
1151   bool res = false;
1152   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end() && !res; ++it )
1153     res = it.data().vis;
1154   return !res;
1155 }
1156
1157 void QtxWorkstackArea::insertWidget( QWidget* wid, const int idx )
1158 {
1159   if ( !wid )
1160     return;
1161
1162   int pos = myList.find( wid );
1163   if ( pos != -1 && ( pos == idx || ( idx < 0 && pos == (int)myList.count() - 1 ) ) )
1164     return;
1165
1166   myList.removeRef( wid );
1167   pos = idx < 0 ? myList.count() : idx;
1168   myList.insert( QMIN( pos, (int)myList.count() ), wid );
1169   if ( !myInfo.contains( wid ) )
1170   {
1171     QtxWorkstackChild* child = new QtxWorkstackChild( wid, myStack );
1172     myChild.insert( wid, child );
1173     myInfo.insert( wid, WidgetInfo() );
1174     myInfo[wid].id = generateId();
1175     myInfo[wid].vis = wid->isVisibleTo( wid->parentWidget() );
1176
1177     connect( child, SIGNAL( destroyed( QObject* ) ), this, SLOT( onChildDestroyed( QObject* ) ) );
1178     connect( wid, SIGNAL( destroyed() ), this, SLOT( onWidgetDestroyed() ) );
1179     connect( child, SIGNAL( shown( QtxWorkstackChild* ) ), this, SLOT( onChildShown( QtxWorkstackChild* ) ) );
1180     connect( child, SIGNAL( hided( QtxWorkstackChild* ) ), this, SLOT( onChildHided( QtxWorkstackChild* ) ) );
1181     connect( child, SIGNAL( activated( QtxWorkstackChild* ) ), this, SLOT( onChildActivated( QtxWorkstackChild* ) ) );
1182     connect( child, SIGNAL( captionChanged( QtxWorkstackChild* ) ), this, SLOT( onChildCaptionChanged( QtxWorkstackChild* ) ) );
1183   }
1184
1185   updateState();
1186
1187   setWidgetActive( wid );
1188 }
1189
1190 void QtxWorkstackArea::onContextMenuRequested( QPoint p )
1191 {
1192   const QtxWorkstackTabBar* bar = ::qt_cast<const QtxWorkstackTabBar*>( sender() );
1193   if ( !bar )
1194     return;
1195
1196   QWidget* wid = 0;
1197   QTab* tab = myBar->tabAt( tabAt( p ) );
1198   if ( tab )
1199     wid = widget( tab->identifier() );
1200
1201   emit contextMenuRequested( wid, p );
1202 }
1203
1204 void QtxWorkstackArea::onWidgetDestroyed()
1205 {
1206   if ( sender() )
1207     removeWidget( (QWidget*)sender(), false );
1208 }
1209
1210 void QtxWorkstackArea::removeWidget( QWidget* wid, const bool del )
1211 {
1212   if ( !myList.contains( wid ) )
1213     return;
1214
1215   if ( myBar->tab( widgetId( wid ) ) )
1216     myBar->removeTab( myBar->tab( widgetId( wid ) ) );
1217   myStack->removeWidget( child( wid ) );
1218
1219   myList.remove( wid );
1220   myInfo.remove( wid );
1221   myChild.remove( wid );
1222
1223   if( del )
1224   {
1225     delete child( wid );
1226     if( myList.isEmpty() )
1227       delete this;
1228     else
1229       updateState();
1230   }
1231   else
1232     updateState();
1233 }
1234
1235 QWidgetList QtxWorkstackArea::widgetList() const
1236 {
1237   QWidgetList lst;
1238   for ( QWidgetListIt it( myList ); it.current(); ++it )
1239   {
1240     if ( widgetVisibility( it.current() ) )
1241       lst.append( it.current() );
1242   }
1243   return lst;
1244 }
1245
1246 QWidget* QtxWorkstackArea::activeWidget() const
1247 {
1248   return widget( myBar->currentTab() );
1249 }
1250
1251 void QtxWorkstackArea::setActiveWidget( QWidget* wid )
1252 {
1253   myBar->setCurrentTab( widgetId( wid ) );
1254 }
1255
1256 bool QtxWorkstackArea::contains( QWidget* wid ) const
1257 {
1258   return myList.contains( wid );
1259 }
1260
1261 void QtxWorkstackArea::show()
1262 {
1263   QMap<QWidget*, bool> map;
1264   for ( QWidgetListIt it( myList ); it.current(); ++it )
1265   {
1266     map.insert( it.current(), isBlocked( it.current() ) );
1267     setBlocked( it.current(), true );
1268   }
1269
1270   QWidget::show();
1271
1272   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1273     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1274 }
1275
1276 void QtxWorkstackArea::hide()
1277 {
1278   QMap<QWidget*, bool> map;
1279   for ( QWidgetListIt it( myList ); it.current(); ++it )
1280   {
1281     map.insert( it.current(), isBlocked( it.current() ) );
1282     setBlocked( it.current(), true );
1283   }
1284
1285   QWidget::hide();
1286
1287   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1288     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1289 }
1290
1291 bool QtxWorkstackArea::isActive() const
1292 {
1293   QtxWorkstack* ws = workstack();
1294   if ( !ws )
1295     return false;
1296
1297   return ws->activeArea() == this;
1298 }
1299
1300 void QtxWorkstackArea::updateActiveState()
1301 {
1302   myBar->setActive( isActive() );
1303 }
1304
1305 QtxWorkstack* QtxWorkstackArea::workstack() const
1306 {
1307   QtxWorkstack* ws = 0;
1308   QWidget* wid = parentWidget();
1309   while ( wid && !ws )
1310   {
1311     if ( wid->inherits( "QtxWorkstack" ) )
1312       ws = (QtxWorkstack*)wid;
1313     wid = wid->parentWidget();
1314   }
1315   return ws;
1316 }
1317
1318 bool QtxWorkstackArea::eventFilter( QObject* o, QEvent* e )
1319 {
1320   if ( o->isWidgetType() )
1321   {
1322     QWidget* wid = (QWidget*)o;
1323     if ( e->type() == QEvent::FocusIn || e->type() == QEvent::MouseButtonPress )
1324     {
1325       bool ok = false;
1326       while ( !ok && wid && wid != myClose )
1327       {
1328         ok = wid == this;
1329         wid = wid->parentWidget();
1330       }
1331       if ( ok )
1332         QApplication::postEvent( this, new QCustomEvent( (QEvent::Type)( e->type() == QEvent::FocusIn ? ActivateWidget : FocusWidget ) ) );
1333     }
1334   }
1335   return false;
1336 }
1337
1338 QRect QtxWorkstackArea::floatRect() const
1339 {
1340   QRect r = myStack->geometry();
1341   return QRect( mapToGlobal( r.topLeft() ), mapToGlobal( r.bottomRight() ) );
1342 }
1343
1344 QRect QtxWorkstackArea::floatTab( const int idx ) const
1345 {
1346   return myBar->tabRect( idx );
1347 }
1348
1349 int QtxWorkstackArea::tabAt( const QPoint& p ) const
1350 {
1351   int idx = -1;
1352   for ( int i = 0; i < myBar->count() && idx == -1; i++ )
1353   {
1354     QRect r = myBar->tabRect( i );
1355     if ( r.isValid() && r.contains( p ) )
1356       idx = i;
1357   }
1358   return idx;
1359 }
1360
1361 void QtxWorkstackArea::customEvent( QCustomEvent* e )
1362 {
1363   switch ( e->type() )
1364   {
1365   case ActivateWidget:
1366     emit activated( activeWidget() );
1367     break;
1368   case FocusWidget:
1369     if ( activeWidget() && !activeWidget()->focusWidget() )
1370       activeWidget()->setFocus();
1371     break;
1372   case RemoveWidget:
1373     removeWidget( (QWidget*)e->data() );
1374     break;
1375   }
1376 }
1377
1378 void QtxWorkstackArea::focusInEvent( QFocusEvent* e )
1379 {
1380   QWidget::focusInEvent( e );
1381
1382   emit activated( activeWidget() );
1383 }
1384
1385 void QtxWorkstackArea::mousePressEvent( QMouseEvent* e )
1386 {
1387   QWidget::mousePressEvent( e );
1388
1389   emit activated( activeWidget() );
1390 }
1391
1392 void QtxWorkstackArea::onClose()
1393 {
1394   QWidget* wid = activeWidget();
1395   if ( wid )
1396     wid->close();
1397 }
1398
1399 void QtxWorkstackArea::onSelected( int id )
1400 {
1401   updateCurrent();
1402
1403   emit activated( activeWidget() );
1404 }
1405
1406 void QtxWorkstackArea::onDragActiveTab()
1407 {
1408   QtxWorkstackChild* c = child( activeWidget() );
1409   if ( !c )
1410     return;
1411
1412   new QtxWorkstackDrag( workstack(), c );
1413 }
1414
1415 void QtxWorkstackArea::onChildDestroyed( QObject* obj )
1416 {
1417   QtxWorkstackChild* child = (QtxWorkstackChild*)obj;
1418   myStack->removeWidget( child );
1419
1420   QWidget* wid = 0;
1421   for ( ChildMap::ConstIterator it = myChild.begin(); it != myChild.end() && !wid; ++it )
1422   {
1423     if ( it.data() == child )
1424       wid = it.key();
1425   }
1426
1427   myChild.remove( wid );
1428
1429   QApplication::postEvent( this, new QCustomEvent( (QEvent::Type)RemoveWidget, wid ) );
1430 }
1431
1432 void QtxWorkstackArea::onChildShown( QtxWorkstackChild* c )
1433 {
1434   setWidgetShown( c->widget(), true );
1435 }
1436
1437 void QtxWorkstackArea::onChildHided( QtxWorkstackChild* c )
1438 {
1439   setWidgetShown( c->widget(), false );
1440 }
1441
1442 void QtxWorkstackArea::onChildActivated( QtxWorkstackChild* c )
1443 {
1444   setWidgetActive( c->widget() );
1445 }
1446
1447 void QtxWorkstackArea::onChildCaptionChanged( QtxWorkstackChild* c )
1448 {
1449   updateTab( c->widget() );
1450 }
1451
1452 void QtxWorkstackArea::updateCurrent()
1453 {
1454   QMap<QWidget*, bool> map;
1455   for ( QWidgetListIt it( myList ); it.current(); ++it )
1456   {
1457     map.insert( it.current(), isBlocked( it.current() ) );
1458     setBlocked( it.current(), true );
1459   }
1460
1461   myStack->raiseWidget( myBar->currentTab() );
1462
1463   for ( QWidgetListIt itr( myList ); itr.current(); ++itr )
1464     setBlocked( itr.current(), map.contains( itr.current() ) ? map[itr.current()] : false );
1465 }
1466
1467 void QtxWorkstackArea::updateTab( QWidget* wid )
1468 {
1469   QTab* tab = myBar->tab( widgetId( wid ) );
1470   if ( !tab )
1471     return;
1472
1473   QIconSet icoSet;
1474   if ( wid->icon() )
1475   {
1476     QPixmap pix = *wid->icon();
1477     pix.convertFromImage( pix.convertToImage().smoothScale( pix.width(), 16, QImage::ScaleMin ) );
1478     icoSet = QIconSet( pix );
1479   }
1480
1481   tab->setIconSet( icoSet );
1482   tab->setText( wid->caption() );
1483 }
1484
1485 QWidget* QtxWorkstackArea::widget( const int id ) const
1486 {
1487   QWidget* wid = 0;
1488   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end() && !wid; ++it )
1489   {
1490     if ( it.data().id == id )
1491       wid = it.key();
1492   }
1493   return wid;
1494 }
1495
1496 int QtxWorkstackArea::widgetId( QWidget* wid ) const
1497 {
1498   int id = -1;
1499   if ( myInfo.contains( wid ) )
1500     id = myInfo[wid].id;
1501   return id;
1502 }
1503
1504 bool QtxWorkstackArea::widgetVisibility( QWidget* wid ) const
1505 {
1506   bool res = false;
1507   if ( myInfo.contains( wid ) )
1508     res = myInfo[wid].vis;
1509   return res;
1510 }
1511
1512 void QtxWorkstackArea::setWidgetActive( QWidget* wid )
1513 {
1514   int id = widgetId( wid );
1515   if ( id < 0 )
1516     return;
1517
1518   myBar->setCurrentTab( id );
1519 }
1520
1521 void QtxWorkstackArea::setWidgetShown( QWidget* wid, const bool on )
1522 {
1523   if ( isBlocked( wid ) || !myInfo.contains( wid ) || myInfo[wid].vis == on )
1524     return;
1525
1526   myInfo[wid].vis = on;
1527   updateState();
1528 }
1529
1530 void QtxWorkstackArea::updateState()
1531 {
1532   bool updBar = myBar->isUpdatesEnabled();
1533   bool updStk = myStack->isUpdatesEnabled();
1534   myBar->setUpdatesEnabled( false );
1535   myStack->setUpdatesEnabled( false );
1536
1537   bool block = myBar->signalsBlocked();
1538   myBar->blockSignals( true );
1539
1540   QWidget* prev = activeWidget();
1541
1542   int idx = 0;
1543   for ( QWidgetListIt it( myList ); it.current(); ++it )
1544   {
1545     QWidget* wid = it.current();
1546     int id = widgetId( wid );
1547
1548     if ( id < 0 )
1549       continue;
1550
1551     bool vis = widgetVisibility( wid );
1552
1553     if ( myBar->tab( id ) && ( !vis || myBar->indexOf( id ) != idx ) )
1554       myBar->removeTab( myBar->tab( id ) );
1555
1556     if ( !myBar->tab( id ) && vis )
1557     {
1558       QTab* tab = new QTab( wid->caption() );
1559       myBar->insertTab( tab, idx );
1560       tab->setIdentifier( id );
1561     }
1562
1563     updateTab( wid );
1564
1565     bool block = isBlocked( wid );
1566     setBlocked( wid, true );
1567
1568     QtxWorkstackChild* cont = child( wid );
1569
1570     if ( !vis )
1571       myStack->removeWidget( cont );
1572     else if ( !myStack->widget( id ) )
1573       myStack->addWidget( cont, id );
1574
1575     if ( vis )
1576       idx++;
1577
1578     setBlocked( wid, block );
1579   }
1580
1581   int curId = widgetId( prev );
1582   if ( !myBar->tab( curId ) )
1583   {
1584     QWidget* wid = 0;
1585     int pos = myList.find( prev );
1586     for ( int i = pos - 1; i >= 0 && !wid; i-- )
1587     {
1588       if ( widgetVisibility( myList.at( i ) ) )
1589         wid = myList.at( i );
1590     }
1591
1592     for ( int j = pos + 1; j < (int)myList.count() && !wid; j++ )
1593     {
1594       if ( widgetVisibility( myList.at( j ) ) )
1595         wid = myList.at( j );
1596     }
1597
1598     if ( wid )
1599       curId = widgetId( wid );
1600   }
1601
1602   myBar->setCurrentTab( curId );
1603
1604   myBar->blockSignals( block );
1605
1606   updateCurrent();
1607
1608   myBar->setUpdatesEnabled( updBar );
1609   myStack->setUpdatesEnabled( updStk );
1610   if ( updBar )
1611     myBar->update();
1612   if ( updStk )
1613     myStack->update();
1614
1615   QResizeEvent re( myBar->size(), myBar->size() );
1616   QApplication::sendEvent( myBar, &re );
1617
1618   if ( isEmpty() )
1619   {
1620     hide();
1621     emit deactivated( this );
1622   }
1623   else
1624   {
1625     show();
1626     if ( prev != activeWidget() )
1627       emit activated( activeWidget() );
1628   }
1629 }
1630
1631 int QtxWorkstackArea::generateId() const
1632 {
1633   QMap<int, int> map;
1634
1635   for ( WidgetInfoMap::ConstIterator it = myInfo.begin(); it != myInfo.end(); ++it )
1636     map.insert( it.data().id, 0 );
1637
1638   int id = 0;
1639   while ( map.contains( id ) )
1640     id++;
1641
1642   return id;
1643 }
1644
1645 bool QtxWorkstackArea::isBlocked( QWidget* wid ) const
1646 {
1647   return myBlock.contains( wid );
1648 }
1649
1650 void QtxWorkstackArea::setBlocked( QWidget* wid, const bool on )
1651 {
1652   if ( on )
1653     myBlock.insert( wid, 0 );
1654   else
1655     myBlock.remove( wid );
1656 }
1657
1658 QtxWorkstackChild* QtxWorkstackArea::child( QWidget* wid ) const
1659 {
1660   QtxWorkstackChild* res = 0;
1661   if ( myChild.contains( wid ) )
1662     res = myChild[wid];
1663   return res;
1664 }
1665
1666 /*!
1667     Class: QtxWorkstackChild [Internal]
1668     Descr:
1669 */
1670
1671 QtxWorkstackChild::QtxWorkstackChild( QWidget* wid, QWidget* parent )
1672 : QHBox( parent ),
1673 myWidget( wid )
1674 {
1675   myWidget->reparent( this, QPoint( 0, 0 ), myWidget->isVisibleTo( myWidget->parentWidget() ) );
1676   myWidget->installEventFilter( this );
1677
1678   connect( myWidget, SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
1679 }
1680
1681 QtxWorkstackChild::~QtxWorkstackChild()
1682 {
1683   qApp->removeEventFilter( this );
1684
1685   if ( !widget() )
1686     return;
1687
1688   widget()->removeEventFilter( this );
1689   widget()->reparent( 0, QPoint( 0, 0 ), false );
1690   disconnect( widget(), SIGNAL( destroyed( QObject* ) ), this, SLOT( onDestroyed( QObject* ) ) );
1691 }
1692
1693 QWidget* QtxWorkstackChild::widget() const
1694 {
1695   return myWidget;
1696 }
1697
1698 bool QtxWorkstackChild::eventFilter( QObject* o, QEvent* e )
1699 {
1700   if ( o->isWidgetType() )
1701   {
1702     if ( e->type() == QEvent::CaptionChange || e->type() == QEvent::IconChange )
1703       emit captionChanged( this );
1704
1705     if ( !e->spontaneous() && ( e->type() == QEvent::Show || e->type() == QEvent::ShowToParent ) )
1706       emit shown( this );
1707
1708     if ( !e->spontaneous() && ( e->type() == QEvent::Hide || e->type() == QEvent::HideToParent ) )
1709       emit hided( this );
1710
1711     if ( e->type() == QEvent::FocusIn )
1712       emit activated( this );
1713   }
1714   return QHBox::eventFilter( o, e );
1715 }
1716
1717 void QtxWorkstackChild::onDestroyed( QObject* obj )
1718 {
1719   if ( obj != widget() )
1720     return;
1721
1722   myWidget = 0;
1723   deleteLater();
1724 }
1725
1726 void QtxWorkstackChild::childEvent( QChildEvent* e )
1727 {
1728   if ( e->type() == QEvent::ChildRemoved && e->child() == widget() )
1729   {
1730     myWidget = 0;
1731     deleteLater();
1732   }
1733   QHBox::childEvent( e );
1734 }
1735
1736 /*!
1737     Class: QtxWorkstackTabBar [Internal]
1738     Descr:
1739 */
1740
1741 QtxWorkstackTabBar::QtxWorkstackTabBar( QWidget* parent )
1742 : QTabBar( parent ),
1743 myId( -1 )
1744 {
1745 }
1746
1747 QtxWorkstackTabBar::~QtxWorkstackTabBar()
1748 {
1749 }
1750
1751 void QtxWorkstackTabBar::setActive( const bool on )
1752 {
1753   QFont aFont = font();
1754   aFont.setUnderline( on );
1755   setFont( aFont );
1756
1757   update();
1758 }
1759
1760 QRect QtxWorkstackTabBar::tabRect( const int idx ) const
1761 {
1762   QRect r;
1763   QTab* t = tabAt( idx );
1764   if ( t )
1765   {
1766     r = t->rect();
1767     r.setLeft( QMAX( r.left(), 0 ) );
1768
1769     int x1 = tabAt( 0 )->rect().left();
1770     int x2 = tabAt( count() - 1 )->rect().right();
1771
1772     int bw = 0;
1773     if ( QABS( x2 - x1 ) > width() )
1774 #if defined QT_VERSION && QT_VERSION >= 0x30300
1775       bw = 2 * style().pixelMetric( QStyle::PM_TabBarScrollButtonWidth, this );
1776 #else
1777       bw = 2 * 16;
1778 #endif
1779
1780     int limit = width() - bw;
1781     r.setRight( QMIN( r.right(), limit ) );
1782
1783     r = QRect( mapToGlobal( r.topLeft() ), r.size() );
1784   }
1785   return r;
1786 }
1787
1788 void QtxWorkstackTabBar::mouseMoveEvent( QMouseEvent* e )
1789 {
1790   if ( myId != -1 && !tab( myId )->rect().contains( e->pos() ) )
1791   {
1792     myId = -1;
1793     emit dragActiveTab();
1794   }
1795
1796   QTabBar::mouseMoveEvent( e );
1797 }
1798
1799 void QtxWorkstackTabBar::mousePressEvent( QMouseEvent* e )
1800 {
1801   QTabBar::mousePressEvent( e );
1802
1803   if ( e->button() == LeftButton )
1804     myId = currentTab();
1805 }
1806
1807 void QtxWorkstackTabBar::mouseReleaseEvent( QMouseEvent* e )
1808 {
1809   QTabBar::mouseReleaseEvent( e );
1810
1811   myId = -1;
1812
1813   if ( e->button() == RightButton )
1814     emit contextMenuRequested( e->globalPos() );
1815 }
1816
1817 void QtxWorkstackTabBar::contextMenuEvent( QContextMenuEvent* e )
1818 {
1819   if ( e->reason() != QContextMenuEvent::Mouse )
1820     emit contextMenuRequested( e->globalPos() );
1821 }
1822
1823 void QtxWorkstackTabBar::paintLabel( QPainter* p, const QRect& br, QTab* t, bool has_focus ) const
1824 {
1825   if ( currentTab() != t->identifier() )
1826   {
1827     QFont fnt = p->font();
1828     fnt.setUnderline( false );
1829     p->setFont( fnt );
1830   }
1831   QTabBar::paintLabel( p, br, t, has_focus );
1832 }
1833
1834 /*!
1835     Class: QtxWorkstackDrag [Internal]
1836     Descr:
1837 */
1838
1839 QtxWorkstackDrag::QtxWorkstackDrag( QtxWorkstack* ws, QtxWorkstackChild* child )
1840 : QObject( 0 ),
1841 myWS( ws ),
1842 myTab( -1 ),
1843 myArea( 0 ),
1844 myPainter( 0 ),
1845 myChild( child )
1846 {
1847   qApp->installEventFilter( this );
1848 }
1849
1850 QtxWorkstackDrag::~QtxWorkstackDrag()
1851 {
1852   qApp->removeEventFilter( this );
1853
1854   endDrawRect();
1855 }
1856
1857 bool QtxWorkstackDrag::eventFilter( QObject*, QEvent* e )
1858 {
1859   switch ( e->type() )
1860   {
1861   case QEvent::MouseMove:
1862     updateTarget( ((QMouseEvent*)e)->globalPos() );
1863     break;
1864   case QEvent::MouseButtonRelease:
1865     drawRect();
1866     endDrawRect();
1867     dropWidget();
1868     deleteLater();
1869     break;
1870   default:
1871     return false;
1872   }
1873   return true;
1874 }
1875
1876 void QtxWorkstackDrag::updateTarget( const QPoint& p )
1877 {
1878   int tab = -1;
1879   QtxWorkstackArea* area = detectTarget( p, tab );
1880   setTarget( area, tab );
1881 }
1882
1883 QtxWorkstackArea* QtxWorkstackDrag::detectTarget( const QPoint& p, int& tab ) const
1884 {
1885   if ( p.isNull() )
1886     return 0;
1887
1888   QtxWorkstackArea* area = myWS->areaAt( p );
1889   if ( area )
1890     tab = area->tabAt( p );
1891   return area;
1892 }
1893
1894 void QtxWorkstackDrag::setTarget( QtxWorkstackArea* area, const int tab )
1895 {
1896   if ( !area || ( myArea == area && tab == myTab ) )
1897     return;
1898
1899   startDrawRect();
1900
1901   if ( myArea )
1902     drawRect();
1903
1904   myTab = tab;
1905   myArea = area;
1906
1907   if ( myArea )
1908     drawRect();
1909 }
1910
1911 void QtxWorkstackDrag::dropWidget()
1912 {
1913   if ( myArea )
1914     myArea->insertWidget( myChild->widget(), myTab );
1915 }
1916
1917 void QtxWorkstackDrag::drawRect()
1918 {
1919   if ( !myPainter || !myArea )
1920     return;
1921
1922   QRect r = myArea->floatRect();
1923   int m = myPainter->pen().width();
1924
1925   r.setTop( r.top() + m + 2 );
1926   r.setLeft( r.left() + m + 2 );
1927   r.setRight( r.right() - m - 2 );
1928   r.setBottom( r.bottom() - m - 2 );
1929
1930   myPainter->drawRect( r );
1931
1932   QRect tr = myArea->floatTab( myTab );
1933   tr.setTop( tr.top() + m );
1934   tr.setLeft( tr.left() + m );
1935   tr.setRight( tr.right() - m );
1936   tr.setBottom( tr.bottom() - m );
1937
1938   myPainter->drawRect( tr );
1939 }
1940
1941 void QtxWorkstackDrag::endDrawRect()
1942 {
1943   delete myPainter;
1944   myPainter = 0;
1945 }
1946
1947 void QtxWorkstackDrag::startDrawRect()
1948 {
1949   if ( myPainter )
1950     return;
1951
1952   int scr = QApplication::desktop()->screenNumber( (QWidget*)this );
1953   QWidget* paint_on = QApplication::desktop()->screen( scr );
1954
1955   myPainter = new QPainter( paint_on, true );
1956   myPainter->setPen( QPen( gray, 3 ) );
1957   myPainter->setRasterOp( XorROP );
1958 }