Skip to content

Commit

Permalink
WIP DPI stages v3
Browse files Browse the repository at this point in the history
  • Loading branch information
z3ntu committed May 9, 2024
1 parent fc17bef commit 14b7c9c
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 34 deletions.
55 changes: 39 additions & 16 deletions src/devicewidget/dpisliderwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,31 +113,35 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
auto *stageWidget = new DpiStageWidget(stageNumber, minimumDpi, maximumDpi, dpi, activeStage == stageNumber);
stageWidget->setSyncDpi(isSynced);

connect(stageWidget, &DpiStageWidget::stageActivated, this, [=]() {
connect(stageWidget, &DpiStageWidget::stageActivated, this, [=](int stageNumber) {
qDebug() << "stage active=" << stageNumber; // FIXME remove
for (int i = 0; i < dpiStageWidgets.size(); i++) {
/* Set all other stages as not active (loop is 0-based) */
if (i + 1 == stageNumber)
continue;
dpiStageWidgets[i]->setStageActive(false);
}
activeStage = stageNumber;
for (DpiStageWidget *widget : dpiStageWidgets) {
widget->informStageActive(activeStage);
}

device->setDPIStages(activeStage, dpiStages);
});

connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](openrazer::RazerDPI dpi) {
dpiStages.replace(stageNumber - 1, dpi);
qDebug() << dpiStages;

handleLastStageEnabled();
connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](int stageNumber, openrazer::RazerDPI dpi) {
handleStageUpdates();

/* Apply to device */
if (singleStage) {
qDebug() << "singleStage dpi=" << dpi; // FIXME remove
device->setDPI(dpi);
} else {
qDebug() << "stage=" << stageNumber << "dpi changed=" << dpi; // FIXME remove

/* If the currently active stage was disabled, we need to
* find a new one to enable */
if (dpi.dpi_x == 0 && dpi.dpi_y == 0 && stageNumber == activeStage) {
activeStage = 1;
for (DpiStageWidget *widget : dpiStageWidgets) {
widget->informStageActive(activeStage);
}
}

device->setDPIStages(activeStage, dpiStages);
}
});
Expand All @@ -147,7 +151,7 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
dpiStageWidgets.append(stageWidget);
}

handleLastStageEnabled();
handleStageUpdates();
} else {
openrazer::RazerDPI currentDpi = { 0, 0 };
try {
Expand All @@ -163,7 +167,7 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
auto *stageWidget = new DpiStageWidget(0, minimumDpi, maximumDpi, currentDpi, false);
stageWidget->setSingleStage(true);
stageWidget->setSyncDpi(isSynced);
connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](openrazer::RazerDPI dpi) {
connect(stageWidget, &DpiStageWidget::dpiChanged, this, [=](int /*stageNumber*/, openrazer::RazerDPI dpi) {
qDebug() << "single-stage dpi changed=" << dpi;
device->setDPI(dpi);
});
Expand All @@ -174,8 +178,24 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
}
}

void DpiSliderWidget::handleLastStageEnabled()
void DpiSliderWidget::handleStageUpdates()
{
/* Re-number the stages to account for disabled stages, re-init dpiStages
* array based on new values */
int stageNumber = 1;
dpiStages.clear();
for (DpiStageWidget *stageWidget : dpiStageWidgets) {
openrazer::RazerDPI dpi = stageWidget->getDpi();
if (dpi.dpi_x != 0 && dpi.dpi_y != 0)
dpiStages.insert(stageNumber - 1, dpi);

/* Set the stage number, if it returns false the stage is currently
* disabled and we'll try the same number on the next stage */
if (stageWidget->setStageNumber(stageNumber)) {
stageNumber++;
}
}

/* Disable "Enable" button if last stage active */
int nrDisabledStages = 0;
for (openrazer::RazerDPI dpiStage : dpiStages) {
Expand All @@ -184,8 +204,11 @@ void DpiSliderWidget::handleLastStageEnabled()
}
}
bool lastStage = nrDisabledStages == dpiStages.size() - 1;
qDebug() << "nrDisabledStages=" << nrDisabledStages << "lastStage=" << lastStage;
for (DpiStageWidget *stageWidget : dpiStageWidgets) {
stageWidget->informLastStage(lastStage);
}
qDebug() << "nrDisabledStages=" << nrDisabledStages << "lastStage=" << lastStage; // FIXME remove

qDebug() << "nr of dpi stages after handleStageUpdates:" << dpiStages.size();
qDebug() << "stages:" << dpiStages;
}
2 changes: 1 addition & 1 deletion src/devicewidget/dpisliderwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class DpiSliderWidget : public QWidget

QVector<DpiStageWidget *> dpiStageWidgets;

void handleLastStageEnabled();
void handleStageUpdates();
};

#endif // DPISLIDERWIDGET_H
43 changes: 30 additions & 13 deletions src/devicewidget/dpistagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
#include <QHBoxLayout>
#include <QVBoxLayout>

DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent)
DpiStageWidget::DpiStageWidget(int initialStageNumber, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent)
: QWidget(parent)
{
this->stageNumber = stage;
this->stageNumber = initialStageNumber;

auto *dpiStageHBox = new QHBoxLayout(this);

dpiStageButton = new QPushButton();
dpiStageButton->setText(QString::number(stage));
dpiStageButton->setText(QString::number(stageNumber));
dpiStageButton->setCheckable(true);
dpiStageButton->setChecked(activeStage);
dpiStageButton->setMaximumWidth(40);
Expand All @@ -28,7 +28,7 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openra
return;
}

emit stageActivated();
emit stageActivated(stageNumber);
});

// HBoxes
Expand Down Expand Up @@ -101,10 +101,10 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openra
connect(dpiXSlider, &QSlider::valueChanged, this, [=](int sliderValue) {
if (syncDpi) {
dpiYSlider->setValue(sliderValue);
qDebug() << "set dpi x=" << sliderValue * 100 << "y=" << sliderValue * 100;
// qDebug() << "set dpi x=" << sliderValue * 100 << "y=" << sliderValue * 100;
emitDpiChanged();
} else {
qDebug() << "set dpi x=" << sliderValue * 100;
// qDebug() << "set dpi x=" << sliderValue * 100;
emitDpiChanged();
}
dpiXSpinBox->setValue(sliderValue * 100);
Expand All @@ -113,7 +113,7 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openra
if (syncDpi) {
dpiXSlider->setValue(sliderValue);
} else {
qDebug() << "set dpi y=" << sliderValue * 100;
// qDebug() << "set dpi y=" << sliderValue * 100;
emitDpiChanged();
}
dpiYSpinBox->setValue(sliderValue * 100);
Expand All @@ -128,7 +128,7 @@ void DpiStageWidget::setSyncDpi(bool syncDpi)
int dpiXSliderValue = dpiXSlider->value();
if (syncDpi && dpiXSliderValue != dpiYSlider->value()) {
dpiYSlider->setValue(dpiXSliderValue);
qDebug() << "sync - set dpi x=" << dpiXSliderValue * 100 << "y=" << dpiXSliderValue * 100;
// qDebug() << "sync - set dpi x=" << dpiXSliderValue * 100 << "y=" << dpiXSliderValue * 100;
emitDpiChanged();
}

Expand Down Expand Up @@ -161,9 +161,9 @@ void DpiStageWidget::setSingleStage(bool singleStage)
}
}

void DpiStageWidget::setStageActive(bool activeStage)
void DpiStageWidget::informStageActive(int activeStage)
{
dpiStageButton->setChecked(activeStage);
dpiStageButton->setChecked(activeStage == stageNumber && dpiStageButton->isEnabled());
}

void DpiStageWidget::informLastStage(bool lastStage)
Expand All @@ -176,16 +176,33 @@ void DpiStageWidget::informLastStage(bool lastStage)
}
}

void DpiStageWidget::emitDpiChanged()
bool DpiStageWidget::setStageNumber(int stageNumber)
{
if (!enableCheckBox->isChecked())
return false;

this->stageNumber = stageNumber;
dpiStageButton->setText(QString::number(stageNumber));
return true;
}

openrazer::RazerDPI DpiStageWidget::getDpi()
{
openrazer::RazerDPI dpi = { 0, 0 };

/* Only provide the value if the stage is enabled */
if (enableCheckBox->isChecked())
dpi = { static_cast<ushort>(dpiXSlider->value() * 100),
static_cast<ushort>(dpiYSlider->value() * 100) };
qDebug() << "emit dpi changed=" << dpi;
emit dpiChanged(dpi);

return dpi;
}

void DpiStageWidget::emitDpiChanged()
{
openrazer::RazerDPI dpi = getDpi();
// qDebug() << "emit dpi changed=" << dpi;
emit dpiChanged(stageNumber, dpi);
}

void DpiStageWidget::updateEnabled(bool enabled)
Expand Down
22 changes: 18 additions & 4 deletions src/devicewidget/dpistagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,30 @@ class DpiStageWidget : public QWidget
{
Q_OBJECT
public:
DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent = nullptr);
DpiStageWidget(int initialStageNumber, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent = nullptr);

/* Tell the stage that syncing DPI is active - so the Y axis can get hidden */
void setSyncDpi(bool syncDpi);
/* Tell the stage that it's being used as single-stage DPI widget */
void setSingleStage(bool singleStage);
void setStageActive(bool activeStage);
/* Inform the stage which stage should be active */
void informStageActive(int activeStage);
/* Inform the stage that it's the last one that's active and needs to
* disable the 'Enable' button because it needs to stay active */
void informLastStage(bool lastStage);
/* Set the number of this stage - but returns false if it's currently
* disabled */
bool setStageNumber(int stageNumber);
/* Return the currently selected DPI - if the stage is disabled then pass
* the DPI {0,0} */
openrazer::RazerDPI getDpi();

signals:
void dpiChanged(openrazer::RazerDPI dpi);
void stageActivated();
/* The DPI of this stage have changed - if the stage is disabled then
* pass the DPI {0,0} */
void dpiChanged(int stageNumber, openrazer::RazerDPI dpi);
/* This stage has been activated */
void stageActivated(int stageNumber);

private:
QPushButton *dpiStageButton;
Expand Down

0 comments on commit 14b7c9c

Please sign in to comment.