/library/tulip-gui/src/TulipFontIconDialog.cpp

https://github.com/Tulip-Dev/tulip · C++ · 127 lines · 83 code · 26 blank · 18 comment · 10 complexity · 483c201b65381168df8f6186f0d2331b MD5 · raw file

  1. /**
  2. *
  3. * This file is part of Tulip (http://tulip.labri.fr)
  4. *
  5. * Authors: David Auber and the Tulip development Team
  6. * from LaBRI, University of Bordeaux
  7. *
  8. * Tulip is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License
  10. * as published by the Free Software Foundation, either version 3
  11. * of the License, or (at your option) any later version.
  12. *
  13. * Tulip is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. */
  19. #include <tulip/TulipFontIconDialog.h>
  20. #include <tulip/TulipFontIconEngine.h>
  21. #include <tulip/TulipFontAwesome.h>
  22. #include <tulip/TulipMaterialDesignIcons.h>
  23. #include <tulip/TlpQtTools.h>
  24. #include <tulip/TulipSettings.h>
  25. #include <QRegExp>
  26. #include <QDesktopServices>
  27. #include <QUrl>
  28. #include "ui_TulipFontIconDialog.h"
  29. using namespace tlp;
  30. TulipFontIconDialog::TulipFontIconDialog(QWidget *parent)
  31. : QDialog(parent), _ui(new Ui::TulipFontIconDialog) {
  32. _ui->setupUi(this);
  33. _ui->iconsCreditLabel->setText(
  34. QString("<p style=\" font-size:11px;\">Special credit for the design "
  35. "of icons goes to:<br/><b>Font "
  36. "Awesome </b><a "
  37. "href=\"http://fontawesome.com\"><span "
  38. "style=\"color:#0d47f1;\">fontawesome.com</span></a> "
  39. "(v%1)<br/><b>Material Design Icons </b>"
  40. "<a "
  41. "href=\"https://materialdesignicons.com\"><span "
  42. "style=\"color:#0d47f1;\">materialdesignicons.com</span></"
  43. "a> (v%2)</p>")
  44. .arg(TulipFontAwesome::getVersion().c_str())
  45. .arg(TulipMaterialDesignIcons::getVersion().c_str()));
  46. connect(_ui->iconNameFilterLineEdit, SIGNAL(textChanged(const QString &)), this,
  47. SLOT(updateIconList()));
  48. connect(_ui->iconsCreditLabel, SIGNAL(linkActivated(const QString &)), this,
  49. SLOT(openUrlInBrowser(const QString &)));
  50. updateIconList();
  51. }
  52. void TulipFontIconDialog::updateIconList() {
  53. _ui->iconListWidget->clear();
  54. QRegExp regexp(_ui->iconNameFilterLineEdit->text());
  55. std::vector<std::string> iconNames = TulipFontAwesome::getSupportedIcons();
  56. bool darkMode = TulipSettings::isDisplayInDarkMode();
  57. for (auto &it : iconNames) {
  58. QString iconName = tlpStringToQString(it);
  59. if (regexp.indexIn(iconName) != -1) {
  60. _ui->iconListWidget->addItem(
  61. new QListWidgetItem(TulipFontIconEngine::icon(it, darkMode), iconName));
  62. }
  63. }
  64. iconNames = TulipMaterialDesignIcons::getSupportedIcons();
  65. for (auto &it : iconNames) {
  66. QString iconName = tlpStringToQString(it);
  67. if (regexp.indexIn(iconName) != -1) {
  68. _ui->iconListWidget->addItem(
  69. new QListWidgetItem(TulipFontIconEngine::icon(it, darkMode), iconName));
  70. }
  71. }
  72. if (_ui->iconListWidget->count() > 0) {
  73. _ui->iconListWidget->setCurrentRow(0);
  74. }
  75. }
  76. QString TulipFontIconDialog::getSelectedIconName() const {
  77. return _selectedIconName;
  78. }
  79. void TulipFontIconDialog::setSelectedIconName(const QString &iconName) {
  80. QList<QListWidgetItem *> items = _ui->iconListWidget->findItems(iconName, Qt::MatchExactly);
  81. if (!items.isEmpty()) {
  82. _ui->iconListWidget->setCurrentItem(items.at(0));
  83. _selectedIconName = iconName;
  84. }
  85. }
  86. void TulipFontIconDialog::accept() {
  87. if (_ui->iconListWidget->count() > 0) {
  88. _selectedIconName = _ui->iconListWidget->currentItem()->text();
  89. }
  90. QDialog::accept();
  91. }
  92. void TulipFontIconDialog::showEvent(QShowEvent *ev) {
  93. QDialog::showEvent(ev);
  94. _selectedIconName = _ui->iconListWidget->currentItem()->text();
  95. if (parentWidget())
  96. move(parentWidget()->window()->frameGeometry().topLeft() +
  97. parentWidget()->window()->rect().center() - rect().center());
  98. }
  99. void TulipFontIconDialog::openUrlInBrowser(const QString &url) {
  100. QDesktopServices::openUrl(QUrl(url));
  101. }