Fraxinus  2023.01.05-dev+develop.0da12
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) SINTEF Department of Medical Technology.
5 All rights reserved.
6 
7 CustusX is released under a BSD 3-Clause license.
8 
9 See Lisence.txt (https://github.com/SINTEFMedtek/CustusX/blob/master/License.txt) for details.
10 =========================================================================*/
11 #include "cxControllableSplitter.h"
12 
13 #include <QSplitter>
14 #include <QVBoxLayout>
15 #include <QAction>
16 #include <QTimer>
17 #include "cxLogger.h"
18 
19 namespace cx
20 {
21 
23  mShiftSplitterLeft(NULL),
24  mShiftSplitterRight(NULL),
25  mSplitterRatio(0.5),
26  mOptions(options)
27 {
28  QVBoxLayout* layout = new QVBoxLayout(this);
29  layout->setMargin(0);
30  layout->setSpacing(0);
31 
32  mSplitter = new QSplitter(Qt::Horizontal);
33  connect(mSplitter, &QSplitter::splitterMoved, this, &ControllableSplitter::onSplitterMoved);
34 
35  layout->addWidget(mSplitter, 1);
36 
37  mSplitterRatio = this->getSplitterRatioOption().readValue(QString::number(0.5)).toDouble();
38 
39  // must set geometry after sizes have been set, i.e. after return to the main loop:
40  QTimer::singleShot(0, this, SLOT(initializeSettings()));
41 }
42 
44 {
45  this->getSplitterRatioOption().writeValue(QString::number(mSplitterRatio));
46  this->getShiftStateOption().writeValue(QString::number(this->getShiftState()));
47 }
48 
49 XmlOptionItem ControllableSplitter::getSplitterRatioOption()
50 {
51  return XmlOptionItem("splitter_ratio", mOptions.getElement());
52 }
53 
54 XmlOptionItem ControllableSplitter::getShiftStateOption()
55 {
56  return XmlOptionItem("shift_state", mOptions.getElement());
57 }
58 
59 void ControllableSplitter::addLeftWidget(QWidget *widget, QString name)
60 {
61  mLeftName = name;
62  mSplitter->insertWidget(0, widget);
63 }
64 void ControllableSplitter::addRightWidget(QWidget *widget, QString name)
65 {
66  mRightName = name;
67  mSplitter->insertWidget(1, widget);
68 }
69 
70 void ControllableSplitter::initializeSettings()
71 {
72  this->setShiftState(this->getShiftStateOption().readValue("0").toInt());
73  this->onSplitterMoved();
74 }
75 
77 {
78  if (!mShiftSplitterLeft)
79  {
80  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-left-3.png"),
81  QString("Show %1").arg(mRightName), this);
82  action->setToolTip(QString("Show more %1").arg(mRightName));
83  action->setStatusTip(action->toolTip());
84  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterLeft);
85  mShiftSplitterLeft = action;
86  this->enableActions();
87  }
88  return mShiftSplitterLeft;
89 }
90 
92 {
93  if (!mShiftSplitterRight)
94  {
95  QAction* action = new QAction(QIcon(":/icons/open_icon_library/arrow-right-3.png"),
96  QString("Show %1").arg(mLeftName), this);
97  action->setToolTip(QString("Show more %1").arg(mLeftName));
98  action->setStatusTip(action->toolTip());
99  connect(action, &QAction::triggered, this, &ControllableSplitter::onMoveSplitterRight);
100  mShiftSplitterRight = action;
101  this->enableActions();
102  }
103  return mShiftSplitterRight;
104 }
105 
106 void ControllableSplitter::onMoveSplitterLeft()
107 {
108  this->shiftSplitter(-1);
109 }
110 
111 void ControllableSplitter::onMoveSplitterRight()
112 {
113  this->shiftSplitter(+1);
114 }
115 
116 void ControllableSplitter::onSplitterMoved()
117 {
118  QList<int> sizes = mSplitter->sizes();
119  if (this->splitterShowsBoth())
120  mSplitterRatio = double(sizes[0]) /double(sizes[0]+sizes[1]);
121 
122  this->enableActions();
123 }
124 
125 void ControllableSplitter::enableActions()
126 {
127  if (mShiftSplitterLeft)
128  mShiftSplitterLeft->setEnabled(this->getShiftState()>=0);
129  if (mShiftSplitterRight)
130  mShiftSplitterRight->setEnabled(this->getShiftState()<=0);
131 }
132 
133 bool ControllableSplitter::splitterShowsBoth() const
134 {
135  QList<int> sizes = mSplitter->sizes();
136  return (( sizes.size()==2 )&&( sizes[0]!=0 )&&( sizes[1]!=0 ));
137 }
138 
139 int ControllableSplitter::getShiftState() const
140 {
141  QList<int> sizes = mSplitter->sizes();
142 
143  if(sizes[0]==0)
144  return -1;
145  else if(sizes[1]==0)
146  return 1;
147  else
148  return 0;
149 }
150 
156 void ControllableSplitter::setShiftState(int shiftState)
157 {
158  QList<int> sizes = mSplitter->sizes();
159 
160  if (shiftState<0) // show props
161  {
162  sizes[0] = 0;
163  sizes[1] = 1;
164  }
165  else if (shiftState>0) // show browser
166  {
167  sizes[0] = 1;
168  sizes[1] = 0;
169  }
170  else // show both
171  {
172  int sizesum = sizes[0]+sizes[1];
173  if (sizesum==0) // if size has not been initialized
174  sizesum = 1000;
175  sizes[0] = mSplitterRatio * sizesum;
176  sizes[1] = (1.0-mSplitterRatio) * sizesum;
177  }
178 
179  mSplitter->setSizes(sizes);
180 
181  this->onSplitterMoved();
182 }
183 
184 void ControllableSplitter::shiftSplitter(int shift)
185 {
186  // positive shift axis goes to the right, from browser to properties
187 
188  int shiftState = this->getShiftState();
189  shiftState += shift;
190  this->setShiftState(shiftState);
191 }
192 
193 }//end namespace cx
194 
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.