Skip to content

Commit

Permalink
WIP DPI stages commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
z3ntu committed May 6, 2024
1 parent 477459e commit fc17bef
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 16 deletions.
61 changes: 53 additions & 8 deletions src/devicewidget/dpisliderwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
auto *dpiStagesCheckbox = new QCheckBox();
dpiStagesCheckbox->setText(tr("Enable stages"));
dpiStagesCheckbox->setChecked(true); // FIXME determine based on something
singleStage = false; // FIXME from checkbox
dpiHeaderHBox->addWidget(dpiStagesCheckbox);

connect(dpiStagesCheckbox, &QCheckBox::clicked, this, [=](bool checked) {
Expand All @@ -52,6 +53,9 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
}

dpiStageWidgets[0]->setSingleStage(!checked);
singleStage = !checked;

// FIXME apply current stages / current dpi directly
});
}

Expand Down Expand Up @@ -84,45 +88,66 @@ DpiSliderWidget::DpiSliderWidget(QWidget *parent, libopenrazer::Device *device)
} catch (const libopenrazer::DBusException &e) {
qWarning("Failed to get dpi stages");
}
uchar activeStage = stagesPair.first;
QVector<openrazer::RazerDPI> stages = stagesPair.second;
activeStage = stagesPair.first;
dpiStages = stagesPair.second;

// Assume user wants DPI synced if all values are currently equal
bool isSynced = true;
for (openrazer::RazerDPI dpi : stages) {
for (openrazer::RazerDPI dpi : dpiStages) {
isSynced &= dpi.dpi_x == dpi.dpi_y;
}
dpiSyncCheckbox->setChecked(isSynced);

qDebug() << "activeStage:" << activeStage; // FIXME remove
qDebug() << "stages:" << stages; // FIXME remove
qDebug() << "stages:" << dpiStages; // FIXME remove

/* Create widgets for the 5 possible DPI stages */
for (int stageNumber = 1; stageNumber <= 5; stageNumber++) {
/* Get DPI for current stage - or default value */
openrazer::RazerDPI dpi = stages.value(stageNumber - 1);
/* Makes sure we have a DPI stage for every value - 0/0 if not provided */
if (dpiStages.size() < stageNumber) {
dpiStages.append({ 0, 0 });
}
/* Get DPI for current stage */
openrazer::RazerDPI dpi = dpiStages[stageNumber - 1];

auto *stageWidget = new DpiStageWidget(stageNumber, minimumDpi, maximumDpi, dpi, activeStage == stageNumber);
stageWidget->setSyncDpi(isSynced);

connect(stageWidget, &DpiStageWidget::stageActivated, this, [=]() {
qDebug() << "stage active=" << 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;

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

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

handleLastStageEnabled();

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

verticalLayout->addWidget(stageWidget);

dpiStageWidgets.append(stageWidget);
}

handleLastStageEnabled();
} else {
openrazer::RazerDPI currentDpi = { 0, 0 };
try {
Expand All @@ -138,9 +163,29 @@ 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) {
qDebug() << "single-stage dpi changed=" << dpi;
device->setDPI(dpi);
});

verticalLayout->addWidget(stageWidget);

dpiStageWidgets.append(stageWidget);
}
}

void DpiSliderWidget::handleLastStageEnabled()
{
/* Disable "Enable" button if last stage active */
int nrDisabledStages = 0;
for (openrazer::RazerDPI dpiStage : dpiStages) {
if (dpiStage.dpi_x == 0 && dpiStage.dpi_y == 0) {
nrDisabledStages++;
}
}
bool lastStage = nrDisabledStages == dpiStages.size() - 1;
qDebug() << "nrDisabledStages=" << nrDisabledStages << "lastStage=" << lastStage;
for (DpiStageWidget *stageWidget : dpiStageWidgets) {
stageWidget->informLastStage(lastStage);
}
}
7 changes: 7 additions & 0 deletions src/devicewidget/dpisliderwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ class DpiSliderWidget : public QWidget
private:
libopenrazer::Device *device;

bool singleStage;

uchar activeStage;
QVector<openrazer::RazerDPI> dpiStages;

QVector<DpiStageWidget *> dpiStageWidgets;

void handleLastStageEnabled();
};

#endif // DPISLIDERWIDGET_H
38 changes: 30 additions & 8 deletions src/devicewidget/dpistagewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openrazer::RazerDPI currentDpi, bool activeStage, QWidget *parent)
: QWidget(parent)
{
this->stageNumber = stage;

auto *dpiStageHBox = new QHBoxLayout(this);

dpiStageButton = new QPushButton();
Expand Down Expand Up @@ -77,14 +79,10 @@ DpiStageWidget::DpiStageWidget(int stage, int minimumDpi, int maximumDpi, openra
enableCheckBox->setChecked(currentDpi.dpi_x != 0);
dpiStageHBox->addWidget(enableCheckBox);

dpiXSlider->setEnabled(currentDpi.dpi_x != 0);
dpiYSlider->setEnabled(currentDpi.dpi_x != 0);
dpiStageButton->setEnabled(currentDpi.dpi_x != 0);
updateEnabled(currentDpi.dpi_x != 0);

connect(enableCheckBox, &QCheckBox::clicked, this, [=](bool checked) {
dpiXSlider->setEnabled(checked);
dpiYSlider->setEnabled(checked);
dpiStageButton->setEnabled(checked);
updateEnabled(checked);

/* Emit changed DPI indicating it's now enabled/disabled */
emitDpiChanged();
Expand Down Expand Up @@ -147,15 +145,16 @@ void DpiStageWidget::setSyncDpi(bool syncDpi)
dpiYSpinBox->show();
dpiYSlider->show();
}

// FIXME: emit dpi changed signal?
}

void DpiStageWidget::setSingleStage(bool singleStage)
{
if (singleStage) {
dpiStageButton->hide();
enableCheckBox->hide();
enableCheckBox->setChecked(true);
updateEnabled(true);
emitDpiChanged();
} else {
dpiStageButton->show();
enableCheckBox->show();
Expand All @@ -167,6 +166,16 @@ void DpiStageWidget::setStageActive(bool activeStage)
dpiStageButton->setChecked(activeStage);
}

void DpiStageWidget::informLastStage(bool lastStage)
{
/* We need to disable the checkbox if we're still enabled */
if (lastStage && enableCheckBox->isChecked()) {
enableCheckBox->setEnabled(false);
} else {
enableCheckBox->setEnabled(true);
}
}

void DpiStageWidget::emitDpiChanged()
{
openrazer::RazerDPI dpi = { 0, 0 };
Expand All @@ -178,3 +187,16 @@ void DpiStageWidget::emitDpiChanged()
qDebug() << "emit dpi changed=" << dpi;
emit dpiChanged(dpi);
}

void DpiStageWidget::updateEnabled(bool enabled)
{
dpiXSlider->setEnabled(enabled);
dpiYSlider->setEnabled(enabled);
dpiStageButton->setEnabled(enabled);

if (enabled) {
dpiStageButton->setText(QString::number(stageNumber));
} else {
dpiStageButton->setText("");
}
}
3 changes: 3 additions & 0 deletions src/devicewidget/dpistagewidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class DpiStageWidget : public QWidget
void setSyncDpi(bool syncDpi);
void setSingleStage(bool singleStage);
void setStageActive(bool activeStage);
void informLastStage(bool lastStage);

signals:
void dpiChanged(openrazer::RazerDPI dpi);
Expand All @@ -39,8 +40,10 @@ class DpiStageWidget : public QWidget
QSlider *dpiYSlider;

bool syncDpi;
int stageNumber;

void emitDpiChanged();
void updateEnabled(bool enabled);
};

#endif // DPISTAGEWIDGET_H

0 comments on commit fc17bef

Please sign in to comment.