Skip to content

Commit 4002ac3

Browse files
Merge pull request #123 from trikset/motors-widget-fix
Motors widget fix
2 parents 4e7508a + c35abef commit 4002ac3

File tree

5 files changed

+50
-19
lines changed

5 files changed

+50
-19
lines changed

trikGui/motorLever.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ MotorLever::MotorLever(const QString &port, trikControl::MotorInterface &motor,
3737
, mPowerStep(10)
3838
, mPower(0)
3939
, mNameLabel(port)
40-
, mPowerLabel("0")
4140
, mOnOffLabel(tr("off"))
4241
{
4342
mMotor.powerOff();
@@ -46,27 +45,24 @@ MotorLever::MotorLever(const QString &port, trikControl::MotorInterface &motor,
4645
mPowerBar.setMinimum(mMinPower);
4746
mPowerBar.setMaximum(mMaxPower);
4847
mPowerBar.setValue(0);
49-
mPowerBar.setTextVisible(false);
48+
mPowerBar.setTextVisible(true);
49+
mPowerBar.setFormat("%v");
5050

5151
mNameLabel.setAlignment(Qt::AlignCenter);
5252
mPowerBar.setAlignment(Qt::AlignCenter);
53-
mPowerLabel.setAlignment(Qt::AlignCenter);
5453
mOnOffLabel.setAlignment(Qt::AlignCenter);
5554

56-
// mPowerLabel and mOnOffLabel can change their widths during work. It will cause mPowerBar
57-
// width change. To prevent it, we set fixed widths for mPowerLabel and mOnOffLabel.
58-
// They are equal to maximum widths of the widgets. For mPowerLabel it is when the label text
59-
// is "-100", for mOnOffLabel - "off".
60-
mPowerLabel.setFixedWidth(40);
55+
// mOnOffLabel can change its width during work. It will cause mPowerBar
56+
// width change. To prevent it, we set fixed width it.
6157
mOnOffLabel.setFixedWidth(48);
6258

6359
mLayout.addWidget(&mNameLabel);
6460
mLayout.addWidget(&mPowerBar);
65-
mLayout.addWidget(&mPowerLabel);
6661
mLayout.addWidget(&mOnOffLabel);
6762
setLayout(&mLayout);
6863

6964
setFocusPolicy(Qt::StrongFocus);
65+
setSizePolicy(QSizePolicy::Expanding, QSizePolicy::QSizePolicy::MinimumExpanding);
7066
}
7167

7268
MotorLever::~MotorLever()
@@ -116,7 +112,6 @@ void MotorLever::setPower(int power)
116112

117113
mPower = power;
118114
mPowerBar.setValue(power);
119-
mPowerLabel.setText(QString::number(power));
120115
if (mIsOn) {
121116
mMotor.setPower(power);
122117
}

trikGui/motorLever.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ class MotorLever : public QWidget
7272
QHBoxLayout mLayout;
7373
QLabel mNameLabel;
7474
QProgressBar mPowerBar;
75-
QLabel mPowerLabel;
7675
QLabel mOnOffLabel;
7776
};
7877

trikGui/motorsWidget.cpp

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,17 @@
1414

1515
#include "motorsWidget.h"
1616

17+
#include <QtCore/QTimer>
1718
#include <QtGui/QKeyEvent>
1819

20+
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
21+
#include <QtGui/QVBoxLayout>
22+
#include <QtGui/QScrollArea>
23+
#else
24+
#include <QtWidgets/QVBoxLayout>
25+
#include <QtWidgets/QScrollArea>
26+
#endif
27+
1928
#include "motorLever.h"
2029

2130
using namespace trikGui;
@@ -28,17 +37,34 @@ MotorsWidget::MotorsWidget(trikControl::BrickInterface &brick
2837
, mPorts(mBrick.motorPorts(type))
2938
, mLevers(mPorts.size())
3039
{
40+
const auto scrollArea = new QScrollArea(this);
41+
scrollArea->setFrameStyle(QFrame::NoFrame);
42+
scrollArea->setWidgetResizable(true);
43+
scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
44+
scrollArea->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
45+
46+
const auto leversWidget = new QWidget(this);
47+
const auto innerLayout = new QVBoxLayout(this);
48+
const auto outerLayout = new QHBoxLayout(this);
49+
3150
mPorts.sort();
3251

3352
int i = 0;
3453
for (const QString &port : mPorts) {
3554
MotorLever *lever = new MotorLever(port, *mBrick.motor(port), this);
36-
mLayout.addWidget(lever);
55+
innerLayout->addWidget(lever);
3756
mLevers[i] = lever;
3857
++i;
3958
}
4059

41-
setLayout(&mLayout);
60+
leversWidget->setLayout(innerLayout);
61+
62+
outerLayout->addWidget(scrollArea);
63+
delete this->layout();
64+
this->setLayout(outerLayout);
65+
scrollArea->setWidget(leversWidget);
66+
67+
QTimer::singleShot(0, this, SLOT(fixLeversPosition()));
4268
}
4369

4470
MotorsWidget::~MotorsWidget()
@@ -84,3 +110,9 @@ void MotorsWidget::keyPressEvent(QKeyEvent *event)
84110
}
85111
}
86112
}
113+
114+
void MotorsWidget::fixLeversPosition()
115+
{
116+
focusNextChild();
117+
focusPreviousChild();
118+
}

trikGui/motorsWidget.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,11 @@
1919
#if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
2020
#include <QtGui/QWidget>
2121
#include <QtGui/QVBoxLayout>
22+
#include <QtGui/QScrollArea>
2223
#else
2324
#include <QtWidgets/QWidget>
2425
#include <QtWidgets/QVBoxLayout>
26+
#include <QtWidgets/QScrollArea>
2527
#endif
2628

2729
#include <QtCore/QString>
@@ -61,9 +63,12 @@ class MotorsWidget : public TrikGuiDialog
6163
protected:
6264
void keyPressEvent(QKeyEvent *event) override;
6365

64-
private:
65-
QVBoxLayout mLayout;
66+
private slots:
67+
/// Hack to work around odd QScrollArea behavior which incorrectly positions inner widget when there is no need for
68+
/// vertical scrollbar.
69+
void fixLeversPosition();
6670

71+
private:
6772
trikControl::BrickInterface &mBrick;
6873
QStringList mPorts;
6974
QVector<MotorLever *> mLevers; // Has ownership.

trikGui/trikGui.pro

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ HEADERS += \
2222
$$PWD/controller.h \
2323
$$PWD/digitSelector.h \
2424
$$PWD/encoderIndicator.h \
25+
$$PWD/fileManagerMessageBox.h \
2526
$$PWD/fileManagerWidget.h \
27+
$$PWD/informationWidget.h \
2628
$$PWD/languageSelectionWidget.h \
2729
$$PWD/lazyMainWidget.h \
2830
$$PWD/lazyMainWidgetWrapper.h \
@@ -37,17 +39,15 @@ HEADERS += \
3739
$$PWD/sensorsSelectionWidget.h \
3840
$$PWD/sensorsWidget.h \
3941
$$PWD/startWidget.h \
42+
$$PWD/systemSettingsWidget.h \
4043
$$PWD/trikGuiApplication.h \
4144
$$PWD/trikGuiDialog.h \
4245
$$PWD/trikGuiMessageBox.h \
46+
$$PWD/updateWidget.h \
4347
$$PWD/wiFiAPWidget.h \
4448
$$PWD/wiFiClientWidget.h \
4549
$$PWD/wiFiInitWidget.h \
4650
$$PWD/wiFiModeWidget.h \
47-
$$PWD/informationWidget.h \
48-
$$PWD/fileManagerMessageBox.h \
49-
$$PWD/updateWidget.h \
50-
$$PWD/systemSettingsWidget.h \
5151

5252
SOURCES += \
5353
$$PWD/backgroundWidget.cpp \

0 commit comments

Comments
 (0)