File: Splitter.cpp

package info (click to toggle)
subcommander 2.0.0~b5p2-5
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 15,344 kB
  • sloc: cpp: 63,594; sh: 4,050; xml: 1,992; makefile: 1,134; ansic: 786; ruby: 251; lisp: 24
file content (197 lines) | stat: -rw-r--r-- 4,019 bytes parent folder | download | duplicates (4)
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
194
195
196
197
/* ====================================================================
 * Copyright (c) 2007-2008  Martin Hauner
 *                          http://subcommander.tigris.org
 *
 * Subcommander is licensed as described in the file doc/COPYING, which
 * you should have received as part of this distribution.
 * ====================================================================
 */

// sc
#include "Splitter.h"
#include "util/String.h"

// qt
#include <QtCore/QEvent>
#include <QtGui/QSplitterHandle>
#include <QtGui/QMoveEvent>
#include <QtGui/QShowEvent>

/** Initial @a position of SplitterHandle. */
const int UnsetPos = -1;

/**
 * Custom splitter handle for @a Splitter widget. Hides or shows the @a First or
 * @a Last widget when double clicking on the handle.
 */
class SplitterHandle : public QSplitterHandle
{
  typedef QSplitterHandle super;

public:
  SplitterHandle( QSplitter* parent, Qt::Orientation orientation, Splitter::Hide hide )
    : super(orientation,parent), _hide(hide), _pos(UnsetPos), _visible(true)
  {
  } 

  void mouseDoubleClickEvent( QMouseEvent* e )
  {
    if( _visible )
    {
      setPos(getCurrentPos());
      moveSplitter(getHidePos());
    }
    else
      moveSplitter(getPos());

    _visible = !_visible;
  }

  /**
   * Get the hide position respecting the @a orientation and @c Splitter::Hide.
   */
  int getHidePos() const
  {
    if( _hide == Splitter::First )
    {
      return 0; 
    }
    else // Splitter::Last
    {
      return getParentSize() - getSize();
    }
  }

  /**
   * Get the size of the handle respecting the @a orientation. Ie. it returns
   * @c width() for @a horizontal and @c height() for @a vertical layout.
   */
  int getSize() const
  {
    if( orientation() == Qt::Vertical )
    {
      return height();
    }
    else // Qt::Horizontal
    {
      return width();
    }
  }

  /**
   * Get the handle position respecting the @a orientation. Ie. it returns
   * @c x() for @a horizontal and @c y() for @a vertical layout.
   */ 
  int getCurrentPos() const
  {
    if( orientation() == Qt::Vertical )
    {
      return y();
    }
    else // Qt::Horizontal
    {
      return x();
    }
  }

  /** Set the click position. */
  void setPos( int pos )
  {
    _pos = pos;
  }

  /** Get the click position. */
  int getPos() const
  {
    return _pos;
  }

  /**
   * Get the size of the handles parent respecting the @a orientation. Ie.
   * it returns @c width() for @a horizontal and @c height() for @a vertical
   * layout.
   */
  int getParentSize() const
  {
    if( orientation() == Qt::Vertical )
    {
      return ((QWidget*)parent())->height();
    }
    else // Qt::Horizontal
    {
      return ((QWidget*)parent())->width();
    }
  }

private:
  Splitter::Hide _hide;

  int            _pos;             ///< visible click position
  bool           _visible;         ///< splitter content is visible
};


///////////////////////////////////////////////////////////////////////////////

Splitter::Splitter( QWidget* parent, Hide hide ) : super(parent), _hide(hide),
_handle(NULL)
{
}

Splitter::Splitter( QWidget* parent, Qt::Orientation orientation, Hide hide )
: super(orientation,parent), _hide(hide), _handle(NULL)
{
}

QSplitterHandle* Splitter::createHandle()
{
  if( count() == 1 )
  {
    _handle = new SplitterHandle( this, orientation(), _hide );
    return _handle;
  }
  else
  {
    return new QSplitterHandle( orientation(), this );
  }
}

void Splitter::showHide( bool b )
{
  // assumes two widget for now...
  QList<int> s;

  if(b)
    s << length()/2 << length()/2;
  else
    s << (_hide == First ? 0 : length())
      << (_hide == First ? length() : 0);

  setSizes(s);
}

void Splitter::enableHide( bool b )
{
  if( _hide == First )
  {
    widget(0)->setEnabled(b);
  }
  else
  {
    widget(count()-1)->setEnabled(b);
  }

  _handle->setEnabled(b);
}

int Splitter::length()
{
  if( orientation() == Qt::Vertical )
  {
    return height();
  }
  else // Qt::Horizontal
  {
    return width();
  }
}