Fraxinus  17.12
An IGT application
cxControllableSplitter.cpp
Go to the documentation of this file.
1 /*=========================================================================
2 This file is part of CustusX, an Image Guided Therapy Application.
3 
4 Copyright (c) 2008-2014, SINTEF Department of Medical Technology
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions are met:
9 
10 1. Redistributions of source code must retain the above copyright notice,
11  this list of conditions and the following disclaimer.
12 
13 2. Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16 
17 3. Neither the name of the copyright holder nor the names of its contributors
18  may be used to endorse or promote products derived from this software
19  without specific prior written permission.
20 
21 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 =========================================================================*/
32 #include "cxControllableSplitter.h"
33 
34 #include <QSplitter>
35 #include <QVBoxLayout>
36 #include <QAction>
37 #include <QTimer>
38 #include "cxLogger.h"
39 
40 namespace cx
41 {
42 
44  mShiftSplitterLeft(NULL),
45  mShiftSplitterRight(NULL),
46  mSplitterRatio(0.5),
47  mOptions(options)
48 {
49  QVBoxLayout* layout = new QVBoxLayout(this);
50  layout->setMargin(0);
51  layout->setSpacing(0);
52 
53  mSplitter = new QSplitter(Qt::Horizontal);
54  connect(mSplitter, &QSplitter::splitterMoved, this, &ControllableSplitter::onSplitterMoved);
55 
56  layout->addWidget(mSplitter, 1);
57 
58  mSplitterRatio = this->getSplitterRatioOption().readValue(QString::number(0.5)).toDouble();
59 
60  // must set geometry after sizes have been set, i.e. after return to the main loop:
61  QTimer::singleShot(0, this, SLOT(initializeSettings()));
62 }
63 
65 {
66  this->getSplitterRatioOption().writeValue(QString::number(mSplitterRatio));
67  this->getShiftStateOption().writeValue(QString::number(this->getShiftState()));
68 }
69 
70 XmlOptionItem ControllableSplitter::getSplitterRatioOption()
71 {
72  return XmlOptionItem("splitter_ratio", mOptions.getElement());
73 }
74 
75 XmlOptionItem ControllableSplitter::getShiftStateOption()
76 {
77  return XmlOptionItem("shift_state", mOptions.getElement());
78 }
79 
80 void ControllableSplitter::addLeftWidget(QWidget *widget, QString name)
81 {
82  mLeftName = name;
83  mSplitter->insertWidget(0, widget);
84 }
85 void ControllableSplitter::addRightWidget(QWidget *widget, QString name)
86 {
87  mRightName = name;
88  mSplitter->insertWidget(1, widget);
89 }
90 
91 void ControllableSplitter::initializeSettings()
92 {
93  this->setShiftState(this->getShiftStateOption().readValue("0").toInt());
94  this->onSplitterMoved();
95 }
96 
98 {
99  if (!mShiftSplitterLeft)
100  {
101  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-left-3.png"),
102  QString("Show %1").arg(mRightName), this);
103  action->setToolTip(QString("Show more %1").arg(mRightName));
104  action->setStatusTip(action->toolTip());
105  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterLeft);
106  mShiftSplitterLeft = action;
107  this->enableActions();
108  }
109  return mShiftSplitterLeft;
110 }
111 
113 {
114  if (!mShiftSplitterRight)
115  {
116  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-right-3.png"),
117  QString("Show %1").arg(mLeftName), this);
118  action->setToolTip(QString("Show more %1").arg(mLeftName));
119  action->setStatusTip(action->toolTip());
120  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterRight);
121  mShiftSplitterRight = action;
122  this->enableActions();
123  }
124  return mShiftSplitterRight;
125 }
126 
127 void ControllableSplitter::onMoveSplitterLeft()
128 {
129  this->shiftSplitter(-1);
130 }
131 
132 void ControllableSplitter::onMoveSplitterRight()
133 {
134  this->shiftSplitter(+1);
135 }
136 
137 void ControllableSplitter::onSplitterMoved()
138 {
139  QList<int> sizes = mSplitter->sizes();
140  if (this->splitterShowsBoth())
141  mSplitterRatio = double(sizes[0]) /double(sizes[0]+sizes[1]);
142 
143  this->enableActions();
144 }
145 
146 void ControllableSplitter::enableActions()
147 {
148  if (mShiftSplitterLeft)
149  mShiftSplitterLeft->setEnabled(this->getShiftState()>=0);
150  if (mShiftSplitterRight)
151  mShiftSplitterRight->setEnabled(this->getShiftState()<=0);
152 }
153 
154 bool ControllableSplitter::splitterShowsBoth() const
155 {
156  QList<int> sizes = mSplitter->sizes();
157  return (( sizes.size()==2 )&&( sizes[0]!=0 )&&( sizes[1]!=0 ));
158 }
159 
160 int ControllableSplitter::getShiftState() const
161 {
162  QList<int> sizes = mSplitter->sizes();
163 
164  if(sizes[0]==0)
165  return -1;
166  else if(sizes[1]==0)
167  return 1;
168  else
169  return 0;
170 }
171 
177 void ControllableSplitter::setShiftState(int shiftState)
178 {
179  QList<int> sizes = mSplitter->sizes();
180 
181  if (shiftState<0) // show props
182  {
183  sizes[0] = 0;
184  sizes[1] = 1;
185  }
186  else if (shiftState>0) // show browser
187  {
188  sizes[0] = 1;
189  sizes[1] = 0;
190  }
191  else // show both
192  {
193  int sizesum = sizes[0]+sizes[1];
194  if (sizesum==0) // if size has not been initialized
195  sizesum = 1000;
196  sizes[0] = mSplitterRatio * sizesum;
197  sizes[1] = (1.0-mSplitterRatio) * sizesum;
198  }
199 
200  mSplitter->setSizes(sizes);
201 
202  this->onSplitterMoved();
203 }
204 
205 void ControllableSplitter::shiftSplitter(int shift)
206 {
207  // positive shift axis goes to the right, from browser to properties
208 
209  int shiftState = this->getShiftState();
210  shiftState += shift;
211  this->setShiftState(shiftState);
212 }
213 
214 }//end namespace cx
215 
void addLeftWidget(QWidget *widget, QString name)
QDomElement getElement()
return the current element
Helper class for storing one string value in an xml document.
void writeValue(const QString &val)
ControllableSplitter(XmlOptionFile options, QWidget *parent)
void addRightWidget(QWidget *widget, QString name)
Helper class for xml files used to store ssc/cx data.
QString readValue(const QString &defval) const
Namespace for all CustusX production code.