Fraxinus  16.5.0-fx-rc9
An IGT application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
39 namespace cx
40 {
41 
43  mShiftSplitterLeft(NULL),
44  mShiftSplitterRight(NULL),
45  mSplitterRatio(0.5),
46  mOptions(options)
47 {
48  QVBoxLayout* layout = new QVBoxLayout(this);
49  layout->setMargin(0);
50  layout->setSpacing(0);
51 
52  mSplitter = new QSplitter(Qt::Horizontal);
53  connect(mSplitter, &QSplitter::splitterMoved, this, &ControllableSplitter::onSplitterMoved);
54 
55  layout->addWidget(mSplitter, 1);
56 
57  mSplitterRatio = this->getSplitterRatioOption().readValue(QString::number(0.5)).toDouble();
58 
59  // must set geometry after sizes have been set, i.e. after return to the main loop:
60  QTimer::singleShot(0, this, SLOT(initializeSettings()));
61 }
62 
64 {
65  this->getSplitterRatioOption().writeValue(QString::number(mSplitterRatio));
66  this->getShiftStateOption().writeValue(QString::number(this->getShiftState()));
67 }
68 
69 XmlOptionItem ControllableSplitter::getSplitterRatioOption()
70 {
71  return XmlOptionItem("splitter_ratio", mOptions.getElement());
72 }
73 
74 XmlOptionItem ControllableSplitter::getShiftStateOption()
75 {
76  return XmlOptionItem("shift_state", mOptions.getElement());
77 }
78 
79 void ControllableSplitter::addLeftWidget(QWidget *widget, QString name)
80 {
81  mLeftName = name;
82  mSplitter->insertWidget(0, widget);
83 }
84 void ControllableSplitter::addRightWidget(QWidget *widget, QString name)
85 {
86  mRightName = name;
87  mSplitter->insertWidget(1, widget);
88 }
89 
90 void ControllableSplitter::initializeSettings()
91 {
92  this->setShiftState(this->getShiftStateOption().readValue("0").toInt());
93  this->onSplitterMoved();
94 }
95 
97 {
98  if (!mShiftSplitterLeft)
99  {
100  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-left-3.png"),
101  QString("Show %1").arg(mRightName), this);
102  action->setToolTip(QString("Show more %1").arg(mRightName));
103  action->setStatusTip(action->toolTip());
104  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterLeft);
105  mShiftSplitterLeft = action;
106  this->enableActions();
107  }
108  return mShiftSplitterLeft;
109 }
110 
112 {
113  if (!mShiftSplitterRight)
114  {
115  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-right-3.png"),
116  QString("Show %1").arg(mLeftName), this);
117  action->setToolTip(QString("Show more %1").arg(mLeftName));
118  action->setStatusTip(action->toolTip());
119  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterRight);
120  mShiftSplitterRight = action;
121  this->enableActions();
122  }
123  return mShiftSplitterRight;
124 }
125 
126 void ControllableSplitter::onMoveSplitterLeft()
127 {
128  this->shiftSplitter(-1);
129 }
130 
131 void ControllableSplitter::onMoveSplitterRight()
132 {
133  this->shiftSplitter(+1);
134 }
135 
136 void ControllableSplitter::onSplitterMoved()
137 {
138  QList<int> sizes = mSplitter->sizes();
139  if (this->splitterShowsBoth())
140  mSplitterRatio = double(sizes[0]) /double(sizes[0]+sizes[1]);
141 
142  this->enableActions();
143 }
144 
145 void ControllableSplitter::enableActions()
146 {
147  if (mShiftSplitterLeft)
148  mShiftSplitterLeft->setEnabled(this->getShiftState()>=0);
149  if (mShiftSplitterRight)
150  mShiftSplitterRight->setEnabled(this->getShiftState()<=0);
151 }
152 
153 bool ControllableSplitter::splitterShowsBoth() const
154 {
155  QList<int> sizes = mSplitter->sizes();
156  return (( sizes.size()==2 )&&( sizes[0]!=0 )&&( sizes[1]!=0 ));
157 }
158 
159 int ControllableSplitter::getShiftState() const
160 {
161  QList<int> sizes = mSplitter->sizes();
162 
163  if(sizes[0]==0)
164  return -1;
165  else if(sizes[1]==0)
166  return 1;
167  else
168  return 0;
169 }
170 
171 void ControllableSplitter::setShiftState(int shiftState)
172 {
173  QList<int> sizes = mSplitter->sizes();
174 
175  if (shiftState<0) // show props
176  {
177  sizes[0] = 0;
178  sizes[1] = 1;
179  }
180  else if (shiftState>0) // show both
181  {
182  sizes[0] = 1;
183  sizes[1] = 0;
184  }
185  else // shop browser
186  {
187  int sizesum = sizes[0]+sizes[1];
188  sizes[0] = mSplitterRatio * sizesum;
189  sizes[1] = (1.0-mSplitterRatio) * sizesum;
190  }
191  mSplitter->setSizes(sizes);
192 
193  this->onSplitterMoved();
194 }
195 
196 void ControllableSplitter::shiftSplitter(int shift)
197 {
198  // positive shift axis goes to the right, from browser to properties
199 
200  int shiftState = this->getShiftState();
201  shiftState += shift;
202  this->setShiftState(shiftState);
203 }
204 
205 }//end namespace cx
206 
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