FolderListSettingCard.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "FolderListSettingCard.h"
  2. #include "Layout/VBoxLayout.h"
  3. #include "DialogBox/Dialog.h"
  4. #include <QFileDialog>
  5. #include <QPainter>
  6. FToolButton::FToolButton(FluentIconBase *icon, const QSize &size, const QSize &iconSize, QWidget *parent)
  7. : QToolButton(parent), m_isPressed(false), m_icon(icon), m_iconSize(iconSize)
  8. {
  9. setFixedSize(size);
  10. }
  11. void FToolButton::mousePressEvent(QMouseEvent *event)
  12. {
  13. m_isPressed = true;
  14. QToolButton::mousePressEvent(event);
  15. }
  16. void FToolButton::mouseReleaseEvent(QMouseEvent *event)
  17. {
  18. m_isPressed = false;
  19. QToolButton::mouseReleaseEvent(event);
  20. }
  21. void FToolButton::paintEvent(QPaintEvent *event)
  22. {
  23. QToolButton::paintEvent(event);
  24. QPainter painter(this);
  25. painter.setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform);
  26. if (m_isPressed) {
  27. painter.setOpacity(0.63);
  28. } else {
  29. painter.setOpacity(1);
  30. }
  31. int w = m_iconSize.width();
  32. int h = m_iconSize.height();
  33. m_icon->render(&painter, QRectF((this->width() - w) / 2, (this->height() - h) / 2, w, h).toRect());
  34. }
  35. FPushButton::FPushButton(FluentIconBase *icon, const QString &text, QWidget *parent)
  36. : QPushButton(parent), m_isPressed(false), m_icon(icon)
  37. {
  38. setText(text);
  39. }
  40. void FPushButton::mousePressEvent(QMouseEvent *event)
  41. {
  42. m_isPressed = true;
  43. QPushButton::mousePressEvent(event);
  44. }
  45. void FPushButton::mouseReleaseEvent(QMouseEvent *event)
  46. {
  47. m_isPressed = false;
  48. QPushButton::mouseReleaseEvent(event);
  49. }
  50. void FPushButton::paintEvent(QPaintEvent *event)
  51. {
  52. QPushButton::paintEvent(event);
  53. QPainter painter(this);
  54. painter.setRenderHints(QPainter::Antialiasing);
  55. if (m_isPressed) {
  56. painter.setOpacity(0.63);
  57. } else {
  58. painter.setOpacity(1);
  59. }
  60. m_icon->render(&painter, QRectF(12, 8, 16, 16).toRect());
  61. }
  62. FolderItem::FolderItem(const QString &folder, QWidget *parent) : QWidget(parent), m_folder(folder)
  63. {
  64. hBoxLayout = new QHBoxLayout(this);
  65. m_folderLabel = new QLabel(folder, this);
  66. m_removeButton = new FToolButton(NEWFLICON(FluentIcon, CLOSE), QSize(39, 29), QSize(12, 12), this);
  67. setFixedHeight(53);
  68. hBoxLayout->setContentsMargins(48, 0, 60, 0);
  69. hBoxLayout->addWidget(m_folderLabel, 0, Qt::AlignLeft);
  70. hBoxLayout->addSpacing(16);
  71. hBoxLayout->addStretch(1);
  72. hBoxLayout->addWidget(m_removeButton, 0, Qt::AlignRight);
  73. hBoxLayout->setAlignment(Qt::AlignVCenter);
  74. connect(m_removeButton, &FToolButton::clicked, [=]() { emit removed(this); });
  75. }
  76. QString FolderItem::folder() const
  77. {
  78. return m_folder;
  79. }
  80. FolderListSettingCard::FolderListSettingCard(const QVariant &configItem, const QString &title, const QString &content,
  81. const QString &directory, QWidget *parent)
  82. : ExpandSettingCard(NEWFLICON(FluentIcon, FOLDER_ADD), title, content, parent),
  83. m_configItem(configItem),
  84. m_dialogDirectory(directory)
  85. {
  86. m_addFolderButton = new FPushButton(NEWFLICON(FluentIcon, FOLDER_ADD), tr("Add folder"), this);
  87. m_folders = m_configItem.toStringList();
  88. initWidget();
  89. }
  90. void FolderListSettingCard::initWidget()
  91. {
  92. addWidget(m_addFolderButton);
  93. // initialize layout
  94. viewLayout->setSpacing(0);
  95. viewLayout->setAlignment(Qt::AlignTop);
  96. viewLayout->setContentsMargins(0, 0, 0, 0);
  97. for (const auto &f : m_folders) {
  98. addFolderItem(f);
  99. }
  100. connect(m_addFolderButton, &FPushButton::clicked, this, &FolderListSettingCard::showFolderDialog);
  101. }
  102. void FolderListSettingCard::addFolderItem(const QString &folder)
  103. {
  104. FolderItem *item = new FolderItem(folder, view());
  105. connect(item, &FolderItem::removed, this, &FolderListSettingCard::showConfirmDialog);
  106. viewLayout->addWidget(item);
  107. adjustViewSize();
  108. }
  109. void FolderListSettingCard::showFolderDialog()
  110. {
  111. QString folder = QFileDialog::getExistingDirectory(this, tr("Choose folder"), m_dialogDirectory);
  112. if (folder.isEmpty() || m_folders.contains(folder)) {
  113. return;
  114. }
  115. addFolderItem(folder);
  116. m_folders.append(folder);
  117. // qconfig.set(self.configItem, self.folders)
  118. emit folderChanged(m_folders);
  119. }
  120. void FolderListSettingCard::showConfirmDialog(FolderItem *item)
  121. {
  122. QFileInfo fi(item->folder());
  123. QString name = fi.baseName();
  124. QString title = tr("Are you sure you want to delete the folder?");
  125. QString content = tr("If you delete the ") + name
  126. + tr(" folder and remove it from the list, the folder will no "
  127. "longer appear in the list, but will not be deleted.");
  128. QScopedPointer<Dialog> w(new Dialog(title, content, this->window()));
  129. connect(w.data(), &Dialog::yesSignal, [=]() { removeFolder(item); });
  130. w->exec();
  131. }
  132. void FolderListSettingCard::removeFolder(FolderItem *item)
  133. {
  134. if (!m_folders.contains(item->folder())) {
  135. return;
  136. }
  137. m_folders.removeAll(item->folder());
  138. viewLayout->deleteWidget(item);
  139. adjustViewSize();
  140. emit folderChanged(m_folders);
  141. // qconfig.set(self.configItem, self.folders)
  142. }