#include "WedgeStyle.h"
#include "WedgeSync.h"
#include <QButtonGroup>
#include <QFont>
#include <QFormLayout>
#include <QFrame>
#include <QHBoxLayout>
#include <QLabel>
#include <QSizePolicy>
#include <QSpacerItem>
#include <QVBoxLayout>
#include <QWidget>
static QSpacerItem* makeSpacer(int height) {
return new QSpacerItem(0, height, QSizePolicy::Minimum, QSizePolicy::Fixed);
}
// radio buttons
static QWidget* makeSegmentRow(QButtonGroup *group, const QString &opt0, const QString &opt1,
int currentIndex, QWidget *parent) {
auto *w = new QWidget(parent);
auto *h = new QHBoxLayout(w);
h->setContentsMargins(0, 0, 0, 0);
h->setSpacing(0);
auto *b0 = new QPushButton(opt0, w);
auto *b1 = new QPushButton(opt1, w);
b0->setProperty("segment", "left");
b1->setProperty("segment", "right");
for (QPushButton *b : {b0, b1}) {
b->setCheckable(true);
b->setCursor(Qt::PointingHandCursor);
b->setFocusPolicy(Qt::NoFocus);
}
group->addButton(b0, 0);
group->addButton(b1, 1);
group->setExclusive(true);
(currentIndex == 1 ? b1 : b0)->setChecked(true);
h->addWidget(b0);
h->addWidget(b1);
h->addStretch(1);
return w;
}
static QWidget* makeSectionHeader(const QString &title) {
auto *w = new QWidget();
auto *h = new QHBoxLayout(w);
h->setContentsMargins(0, 0, 0, 0);
h->setSpacing(8);
auto *label = new QLabel(title);
label->setStyleSheet("color: #999999; font-size: 8pt;");
label->setSizePolicy(QSizePolicy::Maximum, QSizePolicy::Fixed);
h->addWidget(label);
auto *hr = new QFrame();
hr->setFrameShape(QFrame::HLine);
hr->setFrameShadow(QFrame::Plain);
hr->setStyleSheet("color: #cccccc; background-color: #cccccc;");
hr->setFixedHeight(1);
hr->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
h->addWidget(hr, 1);
return w;
}
SyncWindow::SyncWindow(const NoteData &data, QWidget *parent)
: QDialog(parent), originalData(data) {
setWindowTitle("");
setFixedWidth(333);
setStyleSheet(WedgeStyle::sheet());
auto *layout = new QVBoxLayout(this);
layout->setContentsMargins(16, 14, 16, 14);
auto *form = new QFormLayout();
form->setVerticalSpacing(7);
form->setHorizontalSpacing(15);
// sync
routeGroup = new QButtonGroup(this);
QWidget *routeRow = makeSegmentRow(routeGroup, "USB", "WIFI", data.networkRoute, this);
autoGroup = new QButtonGroup(this);
QWidget *autoRow = makeSegmentRow(autoGroup, "True", "False", data.autoMerge ? 0 : 1, this);
QString modifiedStr = (data.lastModified > 0) ? ConfigManager::formatOtc(data.lastModified) : "Never";
QString syncedStr = (data.lastSynced > 0) ? ConfigManager::formatOtc(data.lastSynced) : "Never";
QLabel *modifiedLabel = new QLabel(modifiedStr, this);
modifiedLabel->setStyleSheet("font-family: monospace; font-size: 8pt; padding-top: 4px; padding-bottom: 2px;");
QLabel *syncedLabel = new QLabel(syncedStr, this);
syncedLabel->setStyleSheet("font-family: monospace; font-size: 8pt; padding-top: 2px; padding-bottom: 2px;");
netStatusValLabel = new QLabel("Idle", this);
netStatusValLabel->setStyleSheet("font-family: monospace; font-size: 8pt; padding-top: 2px; padding-bottom: 4px;");
syncBtn = new QPushButton("Send", this);
syncBtn->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
syncBtn->setDefault(true);
QHBoxLayout *netActionLayout = new QHBoxLayout();
netActionLayout->setSpacing(8);
netActionLayout->addStretch(1);
netActionLayout->addWidget(syncBtn, 1);
passKeyEdit = new QLineEdit(this);
passKeyEdit->setText(data.passKey.isEmpty() ? "wedge03569" : data.passKey);
ipEdit = new QLineEdit(this);
ipEdit->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
ipEdit->setPlaceholderText("192.168.1.255");
if (!data.ipAddress.isEmpty()) {
ipEdit->setText(data.ipAddress);
}
form->addRow("Route", routeRow);
form->addRow("Address", ipEdit);
form->addRow("Pass Key", passKeyEdit);
form->addRow("Auto-Merge", autoRow);
form->addRow("Modified", modifiedLabel);
form->addRow("Synced", syncedLabel);
form->addRow("Status", netStatusValLabel);
form->addRow("", netActionLayout);
layout->addLayout(form);
connect(syncBtn, &QPushButton::clicked, this, &SyncWindow::syncRequested);
connect(routeGroup, &QButtonGroup::idClicked, this, [this](int) { emitChange(); });
connect(autoGroup, &QButtonGroup::idClicked, this, [this](int) { emitChange(); });
connect(ipEdit, &QLineEdit::textChanged, this, [this](const QString &) { emitChange(); });
connect(passKeyEdit, &QLineEdit::textChanged, this, [this](const QString &) { emitChange(); });
}
void SyncWindow::emitChange() {
emit dataChanged(getUpdatedData());
}
NoteData SyncWindow::getUpdatedData() const {
NoteData data = originalData;
data.networkRoute = routeGroup->checkedId();
data.autoMerge = (autoGroup->checkedId() == 0);
data.passKey = passKeyEdit->text();
data.ipAddress = ipEdit->text();
return data;
}