FolderListDialog.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. #include "Dialog.h"
  2. #include "FolderListDialog.h"
  3. #include "QFluentWidgets.h"
  4. #include "Common/Icon.h"
  5. #include "Widgets/ScrollArea.h"
  6. #include <QApplication>
  7. #include <QFileDialog>
  8. #include <QFileInfo>
  9. #include <QMouseEvent>
  10. #include <QPainter>
  11. #include <QPushButton>
  12. FolderListDialog::FolderListDialog(const QStringList &folderList, const QString &title, const QString &content,
  13. QWidget *parent)
  14. : MaskDialogBase(parent)
  15. {
  16. m_title = title;
  17. m_content = content;
  18. m_originalPaths = folderList;
  19. m_folderPaths = folderList;
  20. m_vBoxLayout = new QVBoxLayout(widget);
  21. m_titleLabel = new QLabel(title, widget);
  22. m_contentLabel = new QLabel(content, widget);
  23. m_scrollArea = new ScrollArea(widget);
  24. m_scrollWidget = new QWidget(m_scrollArea);
  25. m_completeButton = new QPushButton(tr("Done"), widget);
  26. m_addFolderCard = new AddFolderCard(m_scrollWidget);
  27. for (auto f : m_folderPaths) {
  28. m_folderCards.append(new FolderCard(f, m_scrollWidget));
  29. }
  30. initWidget();
  31. }
  32. FolderListDialog::~FolderListDialog() { }
  33. void FolderListDialog::initWidget()
  34. {
  35. setQss();
  36. int w = qMax(qMax(m_titleLabel->width() + 48, m_contentLabel->width() + 48), 352);
  37. widget->setFixedWidth(w);
  38. m_scrollArea->resize(294, 72);
  39. m_scrollWidget->resize(292, 72);
  40. m_scrollArea->setFixedWidth(294);
  41. m_scrollWidget->setFixedWidth(292);
  42. m_scrollArea->setMaximumHeight(400);
  43. m_scrollArea->setViewportMargins(0, 0, 0, 0);
  44. m_scrollArea->setWidgetResizable(true);
  45. m_scrollArea->setWidget(m_scrollWidget);
  46. m_scrollArea->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
  47. initLayout();
  48. connect(m_addFolderCard, &FolderCard::clicked, this, &FolderListDialog::showFileDialog);
  49. connect(m_completeButton, &QPushButton::clicked, this, &FolderListDialog::onButtonClicked);
  50. for (auto card : m_folderCards) {
  51. connect(card, &FolderCard::clicked, this, &FolderListDialog::showDeleteFolderCardDialog);
  52. }
  53. }
  54. void FolderListDialog::setQss()
  55. {
  56. m_titleLabel->setObjectName("titleLabel");
  57. m_contentLabel->setObjectName("contentLabel");
  58. m_completeButton->setObjectName("completeButton");
  59. m_scrollWidget->setObjectName("scrollWidget");
  60. FluentStyleSheet::apply("FOLDER_LIST_DIALOG", this);
  61. setStyle(QApplication::style());
  62. m_titleLabel->adjustSize();
  63. m_contentLabel->adjustSize();
  64. m_completeButton->adjustSize();
  65. }
  66. void FolderListDialog::initLayout()
  67. {
  68. // initialize layout
  69. m_vBoxLayout->setContentsMargins(24, 24, 24, 24);
  70. m_vBoxLayout->setSizeConstraint(QVBoxLayout::SetFixedSize);
  71. m_vBoxLayout->setAlignment(Qt::AlignTop);
  72. m_vBoxLayout->setSpacing(0);
  73. // labels
  74. QVBoxLayout *layout_1 = new QVBoxLayout();
  75. layout_1->setContentsMargins(0, 0, 0, 0);
  76. layout_1->setSpacing(6);
  77. layout_1->addWidget(m_titleLabel, 0, Qt::AlignTop);
  78. layout_1->addWidget(m_contentLabel, 0, Qt::AlignTop);
  79. m_vBoxLayout->addLayout(layout_1, 0);
  80. m_vBoxLayout->addSpacing(12);
  81. // cards
  82. QHBoxLayout *layout_2 = new QHBoxLayout();
  83. layout_2->setAlignment(Qt::AlignCenter);
  84. layout_2->setContentsMargins(4, 0, 4, 0);
  85. layout_2->addWidget(m_scrollArea, 0, Qt::AlignCenter);
  86. m_vBoxLayout->addLayout(layout_2, 1);
  87. m_vBoxLayout->addSpacing(24);
  88. m_scrollLayout = new QVBoxLayout(m_scrollWidget);
  89. m_scrollLayout->setAlignment(Qt::AlignTop);
  90. m_scrollLayout->setContentsMargins(0, 0, 0, 0);
  91. m_scrollLayout->setSpacing(8);
  92. m_scrollLayout->addWidget(m_addFolderCard, 0, Qt::AlignTop);
  93. for (auto card : m_folderCards) {
  94. m_scrollLayout->addWidget(card, 0, Qt::AlignTop);
  95. }
  96. // buttons
  97. QHBoxLayout *layout_3 = new QHBoxLayout();
  98. layout_3->setContentsMargins(0, 0, 0, 0);
  99. layout_3->addStretch(1);
  100. layout_3->addWidget(m_completeButton);
  101. m_vBoxLayout->addLayout(layout_3, 0);
  102. adjustWidgetSize();
  103. }
  104. void FolderListDialog::adjustWidgetSize()
  105. {
  106. int N = m_folderCards.count();
  107. int h = 72 * (N + 1) + 8 * N;
  108. m_scrollArea->setFixedHeight(qMin(h, 400));
  109. }
  110. /// delete selected folder card
  111. void FolderListDialog::deleteFolderCard(FolderCard *card)
  112. {
  113. m_scrollLayout->removeWidget(card);
  114. int index = m_folderCards.indexOf(card);
  115. m_folderPaths.removeAt(index);
  116. m_folderCards.removeAt(index);
  117. card->deleteLater();
  118. // adjust height
  119. adjustWidgetSize();
  120. }
  121. void FolderListDialog::showFileDialog()
  122. {
  123. QString path = QFileDialog::getExistingDirectory(this, tr("Choose folder"), "./");
  124. if (path.isEmpty() || m_folderPaths.contains(path)) {
  125. return;
  126. }
  127. // create folder card
  128. FolderCard *card = new FolderCard(path, m_scrollWidget);
  129. m_scrollLayout->addWidget(card, 0, Qt::AlignTop);
  130. connect(card, &FolderCard::clicked, this, &FolderListDialog::showDeleteFolderCardDialog);
  131. card->show();
  132. m_folderPaths.append(path);
  133. m_folderCards.append(card);
  134. adjustWidgetSize();
  135. }
  136. void FolderListDialog::onButtonClicked()
  137. {
  138. for (auto f : m_folderPaths) {
  139. if (!m_originalPaths.contains(f)) {
  140. setEnabled(false);
  141. QApplication::processEvents();
  142. emit folderChanged(m_folderPaths);
  143. break;
  144. }
  145. }
  146. close();
  147. }
  148. void FolderListDialog::showDeleteFolderCardDialog()
  149. {
  150. FolderCard *card = qobject_cast<FolderCard *>(sender());
  151. QString title = tr("Are you sure you want to delete the folder?");
  152. QString content = tr("If you delete the ") + card->folderName()
  153. + tr(" folder and remove it from the list, the folder will no longer appear in the list, but will not be "
  154. "deleted.");
  155. Dialog *dialog = new Dialog(title, content, this->window());
  156. connect(dialog, &Dialog::yesSignal, [=]() { deleteFolderCard(card); });
  157. dialog->exec();
  158. static_cast<QDialog *>(dialog)->deleteLater();
  159. }
  160. ClickableWindow::ClickableWindow(QWidget *parent) : QWidget(parent), m_isPressed(false), m_isEnter(false)
  161. {
  162. setAttribute(Qt::WA_TranslucentBackground);
  163. setWindowFlags(Qt::FramelessWindowHint);
  164. setFixedSize(292, 72);
  165. }
  166. void ClickableWindow::mousePressEvent(QMouseEvent * /*event*/)
  167. {
  168. m_isPressed = true;
  169. update();
  170. }
  171. void ClickableWindow::mouseReleaseEvent(QMouseEvent *event)
  172. {
  173. m_isPressed = false;
  174. update();
  175. if (event->button() == Qt::LeftButton) {
  176. emit clicked();
  177. }
  178. }
  179. void ClickableWindow::enterEvent(QEvent * /*event*/)
  180. {
  181. m_isEnter = true;
  182. update();
  183. }
  184. void ClickableWindow::leaveEvent(QEvent * /*event*/)
  185. {
  186. m_isEnter = true;
  187. update();
  188. }
  189. void ClickableWindow::paintEvent(QPaintEvent * /*event*/)
  190. {
  191. QPainter painter(this);
  192. painter.setRenderHints(QPainter::Antialiasing);
  193. bool isDark = QFWIns.isDarkTheme();
  194. int bg = isDark ? 51 : 204;
  195. QBrush brush(QColor(bg, bg, bg));
  196. painter.setPen(Qt::NoPen);
  197. if (!m_isEnter) {
  198. painter.setBrush(brush);
  199. painter.drawRoundedRect(rect(), 4, 4);
  200. } else {
  201. painter.setPen(QPen(QColor(bg, bg, bg), 2));
  202. painter.drawRect(1, 1, width() - 2, height() - 2);
  203. painter.setPen(Qt::NoPen);
  204. if (!m_isPressed) {
  205. bg = isDark ? 24 : 230;
  206. brush.setColor(QColor(bg, bg, bg));
  207. painter.setBrush(brush);
  208. painter.drawRect(2, 2, width() - 4, height() - 4);
  209. } else {
  210. bg = isDark ? 102 : 230;
  211. brush.setColor(QColor(153, 153, 153));
  212. painter.setBrush(brush);
  213. painter.drawRoundedRect(5, 1, width() - 10, height() - 2, 2, 2);
  214. }
  215. }
  216. }
  217. bool ClickableWindow::isEnter() const
  218. {
  219. return m_isEnter;
  220. }
  221. bool ClickableWindow::isPressed() const
  222. {
  223. return m_isPressed;
  224. }
  225. FolderCard::FolderCard(const QString &folderPath, QWidget *parent) : ClickableWindow(parent), m_folderPath(folderPath)
  226. {
  227. QFileInfo fi(m_folderPath);
  228. m_folderName = fi.baseName();
  229. QString c = getIconColor();
  230. m_closeIcon = QPixmap(QString(":/qfluentwidgets/images/folder_list_dialog/Close_%1.png").arg(c))
  231. .scaled(12, 12, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  232. }
  233. void FolderCard::paintEvent(QPaintEvent *event)
  234. {
  235. ClickableWindow::paintEvent(event);
  236. QPainter painter(this);
  237. painter.setRenderHints(QPainter::TextAntialiasing | QPainter::SmoothPixmapTransform | QPainter::Antialiasing);
  238. QColor color = QFWIns.isDarkTheme() ? Qt::white : Qt::black;
  239. painter.setPen(color);
  240. if (isPressed()) {
  241. drawText(&painter, 12, 8, 12, 7);
  242. painter.drawPixmap(width() - 26, 18, m_closeIcon);
  243. } else {
  244. drawText(&painter, 10, 9, 10, 8);
  245. painter.drawPixmap(width() - 24, 20, m_closeIcon);
  246. }
  247. }
  248. void FolderCard::drawText(QPainter *painter, int x1, int fontSize1, int x2, int fontSize2)
  249. {
  250. // paint folder name;
  251. QFont font("Microsoft YaHei", fontSize1, 75);
  252. painter->setFont(font);
  253. QString name = QFontMetrics(font).elidedText(m_folderName, Qt::ElideRight, width() - 48);
  254. painter->drawText(x1, 30, name);
  255. // paint folder path
  256. font = QFont("Microsoft YaHei", fontSize2);
  257. painter->setFont(font);
  258. QString path = QFontMetrics(font).elidedText(m_folderPath, Qt::ElideRight, width() - 24);
  259. painter->drawText(x2, 37, width() - 16, 18, Qt::AlignLeft, path);
  260. }
  261. QString FolderCard::folderName() const
  262. {
  263. return m_folderName;
  264. }
  265. AddFolderCard::AddFolderCard(QWidget *parent) : ClickableWindow(parent)
  266. {
  267. QString c = getIconColor();
  268. m_iconPix = QPixmap(QString(":/qfluentwidgets/images/folder_list_dialog/Add_%1.png").arg(c))
  269. .scaled(22, 22, Qt::KeepAspectRatio, Qt::SmoothTransformation);
  270. }
  271. void AddFolderCard::paintEvent(QPaintEvent *event)
  272. {
  273. ClickableWindow::paintEvent(event);
  274. QPainter painter(this);
  275. int w = width();
  276. int h = height();
  277. int pw = m_iconPix.width();
  278. int ph = m_iconPix.height();
  279. if (isPressed()) {
  280. painter.drawPixmap(int(w / 2 - pw / 2), int(h / 2 - ph / 2), m_iconPix);
  281. } else {
  282. painter.drawPixmap(int(w / 2 - (pw - 4) / 2), int(h / 2 - (ph - 4) / 2), pw - 4, ph - 4, m_iconPix);
  283. }
  284. }