summaryrefslogtreecommitdiffstats
path: root/krusader/Dialogs/percentalsplitter.cpp
blob: ecf6597177fee8961832b95168fae61677849262 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/***************************************************************************
                       percentalsplitter.h  -  description
                             -------------------
    copyright            : (C) 2006 + by Csaba Karai
    e-mail               : krusader@users.sourceforge.net
    web site             : http://krusader.sourceforge.net
 ---------------------------------------------------------------------------
  Description
 ***************************************************************************

  A

     db   dD d8888b. db    db .d8888.  .d8b.  d8888b. d88888b d8888b.
     88 ,8P' 88  `8D 88    88 88'  YP d8' `8b 88  `8D 88'     88  `8D
     88,8P   88oobY' 88    88 `8bo.   88ooo88 88   88 88ooooo 88oobY'
     88`8b   88`8b   88    88   `Y8b. 88~~~88 88   88 88~~~~~ 88`8b
     88 `88. 88 `88. 88b  d88 db   8D 88   88 88  .8D 88.     88 `88.
     YP   YD 88   YD ~Y8888P' `8888Y' YP   YP Y8888D' Y88888P 88   YD

                                                     H e a d e r    F i l e

 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "percentalsplitter.h"
#include <tqtooltip.h>
#include <tqpainter.h>
#include <tqapplication.h>

class PercentalSplitterToolTip : public TQToolTip {
public:
  PercentalSplitterToolTip( TQWidget * parent ) : TQToolTip( parent ) {
  }
  
  virtual ~PercentalSplitterToolTip() {
    remove( parentWidget() );
  }
  
  void maybeTip( const TQPoint & point ) {
    if( parentWidget()->inherits( "PercentalSplitter" ) ) {
      PercentalSplitter *splitter = (PercentalSplitter *)parentWidget();
      
      TQString tipString = splitter->toolTipString();
      TQRect rect = TQRect( parentWidget()->rect() );

      if( splitter->orientation() == Qt::Vertical ) {
        rect.setY( splitter->sizes()[ 0 ] );
        rect.setHeight( splitter->handleWidth() );
      }
      else {
        rect.setX( splitter->sizes()[ 0 ] );
        rect.setWidth( splitter->handleWidth() );
      }
      if( rect.contains( point ) )
        tip( rect, tipString );
    }
  }
};

PercentalSplitter::PercentalSplitter( TQWidget * parent, const char * name ) : TQSplitter( parent, name ), label( 0 ), opaqueOldPos( -1 ) {
  toolTip = new PercentalSplitterToolTip( this );
}  
  
PercentalSplitter::~PercentalSplitter() {
  delete toolTip;
}
  
TQString PercentalSplitter::toolTipString( int p ) {
  TQValueList<int> values = sizes();  
  if( values.count() == 2 && ( values[ 0 ] + values[ 1 ]  != 0 ) ) {
    if( p < 0 )
      p = values[ 0 ];
    int percent = (int)(((double)p / (double)( values[ 0 ] + values[ 1 ] )) * 10000. + 0.5);
    return TQString( "%1.%2%3" ).arg( percent / 100 ).arg( ( percent / 10 )%10 ).arg( percent % 10 ) + "%";
  }
  return TQString();
}
  
void PercentalSplitter::setRubberband ( int p ) {  
  if( p == opaqueOldPos )
    return;

  TQPainter paint( this );
  paint.setPen( gray );
  paint.setBrush( gray );
  paint.setRasterOp( XorROP );
  TQRect r = contentsRect();
  const int rBord = 3; // customizable?
  int hw = handleWidth();
    
  if( orientation() == Qt::Horizontal ) {
    if ( opaqueOldPos >= 0 ) {
      if( label == 0 )
        paint.drawRect( opaqueOldPos + hw / 2 - rBord, r.y(), 2 * rBord, r.height() );
      else {
        TQPoint labelLoc = mapFromGlobal( labelLocation );
        if( labelLoc.y() > r.y() )
          paint.drawRect( opaqueOldPos + hw / 2 - rBord, r.y(), 2 * rBord, labelLoc.y() );
        if( labelLoc.y() + label->height() < r.height() )
          paint.drawRect( opaqueOldPos + hw / 2 - rBord, labelLoc.y() + label->height(), 2 * rBord, r.height() - labelLoc.y() - label->height() );
      }
    }
  } else {
    if ( opaqueOldPos >= 0 ) {
      if( label == 0 )
        paint.drawRect( r.x(), opaqueOldPos + hw / 2 - rBord, r.width(), 2 * rBord );
      else {
        TQPoint labelLoc = mapFromGlobal( labelLocation );
        if( labelLoc.x() > r.x() )
          paint.drawRect( r.x(), opaqueOldPos + hw / 2 - rBord, labelLoc.x(), 2 * rBord );
        if( labelLoc.x() + label->width() < r.width() )
          paint.drawRect( labelLoc.x() + label->width(), opaqueOldPos + hw / 2 - rBord, r.width() - labelLoc.x() - label->width(), 2 * rBord );
      }
    }
  }

  if( p < 0 ) {
    if( label ) {
      delete label;
      label = 0;
    }
  }
  else {
    int scr = TQApplication::desktop()->screenNumber( this );
      
    if( label == 0 ) {
      label = new TQLabel( TQT_TQWIDGET(TQApplication::desktop()->screen( scr )), "SplitterPercent", WStyle_StaysOnTop | 
                          WStyle_Customize | WStyle_NoBorder | WStyle_Tool | WX11BypassWM );
      label->setMargin(1);
      label->setAutoMask( FALSE );
      label->setFrameStyle( TQFrame::Plain | TQFrame::Box );
      label->setLineWidth( 1 );
      label->setAlignment( AlignAuto | AlignTop );
      label->setIndent(0);

      TQFontMetrics fm = label->fontMetrics();
      label->setMinimumWidth( fm.width( "99.99%" ) + 5 );

      label->polish();
    }

    label->setText( toolTipString( p ) );
    label->adjustSize();

    if( orientation() == Qt::Horizontal ) {
      labelLocation = mapToGlobal( TQPoint( p - label->width()/2, r.y() + r.height()/2 ) );
      if( labelLocation.x() < 0 )
        labelLocation.setX( 0 );
    } else {
      labelLocation = mapToGlobal( TQPoint( r.x() + r.width()/2, p - label->height()/2 ) );
      if( labelLocation.y() < 0 )
        labelLocation.setY( 0 );
    }

#ifdef TQ_WS_MAC
    TQRect screen = TQApplication::desktop()->availableGeometry( scr );
#else
    TQRect screen = TQApplication::desktop()->screenGeometry( scr );
#endif

    TQPoint labelLoc = mapFromGlobal( labelLocation );
    if( orientation() == Qt::Horizontal ) {
      if( labelLocation.x() + label->width() > screen.width() )
        labelLocation.setX( screen.width() - label->width() );
      label->move( labelLocation );
      label->show();
    
      if( labelLoc.y() > r.y() )
        paint.drawRect( p + hw / 2 - rBord, r.y(), 2 * rBord, labelLoc.y() );
      if( labelLoc.y() + label->height() < r.height() )
        paint.drawRect( p + hw / 2 - rBord, labelLoc.y() + label->height(), 2 * rBord, r.height() - labelLoc.y() - label->height() );
    } else {
      if( labelLocation.y() + label->height() > screen.height() )
        labelLocation.setY( screen.height() - label->height() );
      label->move( labelLocation );
      label->show();
    
      if( labelLoc.x() > r.x() )
        paint.drawRect( r.x(), p + hw / 2 - rBord, labelLoc.x(), 2 * rBord );
      if( labelLoc.x() + label->width() < r.width() )
        paint.drawRect( labelLoc.x() + label->width(), p + hw / 2 - rBord, r.width() - labelLoc.x() - label->width(), 2 * rBord );
    }
  }
  opaqueOldPos = p;
}

#include "percentalsplitter.moc"