localipv4dialog.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include "localipv4dialog.h"
  2. void LocalIPv4Dialog::setPaneContents() {
  3. mContentsWidget = new QWidget();
  4. QVBoxLayout* contentsLayout = new QVBoxLayout(mContentsWidget);
  5. mContentsWidget->setObjectName("innerPane");
  6. // Adding widgets
  7. QLabel* networkIfaceLabel = new QLabel(tr("Choose network interface:"));
  8. networkIfaceLabel->setFont(mTitleFont);
  9. contentsLayout->addWidget(networkIfaceLabel);
  10. QListWidget* ifaceListWidget = new QListWidget();
  11. ifaceListWidget->setFont(mFont);
  12. QString style = QString("QListView::item:selected { "
  13. "background-color: %1; color: #ffffff; "
  14. "};").arg(getConfigValue("accent").toString());
  15. ifaceListWidget->setStyleSheet(style);
  16. contentsLayout->addWidget(ifaceListWidget);
  17. QHBoxLayout* buttonsLayout = new QHBoxLayout();
  18. buttonsLayout->addSpacerItem(new QSpacerItem(0, 0,
  19. QSizePolicy::MinimumExpanding,
  20. QSizePolicy::Ignored));
  21. QPushButton* cancelButton = new QPushButton(tr("Cancel"));
  22. cancelButton->setFont(mFont);
  23. buttonsLayout->addWidget(cancelButton);
  24. QPushButton* okButton = new QPushButton(tr("OK"));
  25. okButton->setFont(mFont);
  26. buttonsLayout->addWidget(okButton);
  27. contentsLayout->addLayout(buttonsLayout);
  28. // Misc
  29. QList<QNetworkInterface> availableIfaces = QNetworkInterface::allInterfaces();
  30. foreach(QNetworkInterface currentIface, availableIfaces) {
  31. ifaceListWidget->addItem(currentIface.name());
  32. }
  33. // Setting current settings
  34. QString iface = getConfigValue("ipIfname").toString();
  35. for (int i = 0; i < ifaceListWidget->count(); ++i) {
  36. if (!ifaceListWidget->item(i)->text().compare(iface)) {
  37. ifaceListWidget->setCurrentRow(i);
  38. break;
  39. }
  40. }
  41. // Making connections
  42. connect(cancelButton, &QPushButton::clicked, this, [this]() {
  43. this->hide();
  44. delete this;
  45. });
  46. connect(okButton, &QPushButton::clicked, this, [this, ifaceListWidget]() {
  47. prepareToSave(ifaceListWidget);
  48. saveConfig();
  49. this->hide();
  50. delete this;
  51. });
  52. finalizeUI();
  53. setWindowGeometry();
  54. setTransparency(this);
  55. }
  56. void LocalIPv4Dialog::prepareToSave(QListWidget* ifaceListWidget) {
  57. if (!ifaceListWidget->selectedItems().isEmpty()) {
  58. QString ifname = ifaceListWidget->currentItem()->text();
  59. setEntry("ipIfname", ifname);
  60. }
  61. }
  62. LocalIPv4Dialog::LocalIPv4Dialog(QJsonObject* cfgObj) : Dialog(cfgObj,
  63. tr("Local IPv4 Address applet settings"),
  64. "cs-network") {
  65. }