qtpropertybrowserutils.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2016 The Qt Company Ltd.
  4. ** Contact: https://www.qt.io/licensing/
  5. **
  6. ** This file is part of the tools applications of the Qt Toolkit.
  7. **
  8. ** $QT_BEGIN_LICENSE:LGPL$
  9. ** Commercial License Usage
  10. ** Licensees holding valid commercial Qt licenses may use this file in
  11. ** accordance with the commercial license agreement provided with the
  12. ** Software or, alternatively, in accordance with the terms contained in
  13. ** a written agreement between you and The Qt Company. For licensing terms
  14. ** and conditions see https://www.qt.io/terms-conditions. For further
  15. ** information use the contact form at https://www.qt.io/contact-us.
  16. **
  17. ** GNU Lesser General Public License Usage
  18. ** Alternatively, this file may be used under the terms of the GNU Lesser
  19. ** General Public License version 3 as published by the Free Software
  20. ** Foundation and appearing in the file LICENSE.LGPL3 included in the
  21. ** packaging of this file. Please review the following information to
  22. ** ensure the GNU Lesser General Public License version 3 requirements
  23. ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
  24. **
  25. ** GNU General Public License Usage
  26. ** Alternatively, this file may be used under the terms of the GNU
  27. ** General Public License version 2.0 or (at your option) the GNU General
  28. ** Public license version 3 or any later version approved by the KDE Free
  29. ** Qt Foundation. The licenses are as published by the Free Software
  30. ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
  31. ** included in the packaging of this file. Please review the following
  32. ** information to ensure the GNU General Public License requirements will
  33. ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
  34. ** https://www.gnu.org/licenses/gpl-3.0.html.
  35. **
  36. ** $QT_END_LICENSE$
  37. **
  38. ****************************************************************************/
  39. #include "qtpropertybrowserutils_p.h"
  40. #include <QtWidgets/QApplication>
  41. #include <QtGui/QPainter>
  42. #include <QtWidgets/QHBoxLayout>
  43. #include <QtGui/QMouseEvent>
  44. #include <QtWidgets/QCheckBox>
  45. #include <QtWidgets/QLineEdit>
  46. #include <QtWidgets/QMenu>
  47. #include <QtCore/QLocale>
  48. QT_BEGIN_NAMESPACE
  49. QtCursorDatabase::QtCursorDatabase()
  50. {
  51. appendCursor(Qt::ArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Arrow"),
  52. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png")));
  53. appendCursor(Qt::UpArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Up Arrow"),
  54. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png")));
  55. appendCursor(Qt::CrossCursor, QCoreApplication::translate("QtCursorDatabase", "Cross"),
  56. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png")));
  57. appendCursor(Qt::WaitCursor, QCoreApplication::translate("QtCursorDatabase", "Wait"),
  58. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png")));
  59. appendCursor(Qt::IBeamCursor, QCoreApplication::translate("QtCursorDatabase", "IBeam"),
  60. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png")));
  61. appendCursor(Qt::SizeVerCursor, QCoreApplication::translate("QtCursorDatabase", "Size Vertical"),
  62. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png")));
  63. appendCursor(Qt::SizeHorCursor, QCoreApplication::translate("QtCursorDatabase", "Size Horizontal"),
  64. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png")));
  65. appendCursor(Qt::SizeFDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Backslash"),
  66. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png")));
  67. appendCursor(Qt::SizeBDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Slash"),
  68. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png")));
  69. appendCursor(Qt::SizeAllCursor, QCoreApplication::translate("QtCursorDatabase", "Size All"),
  70. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png")));
  71. appendCursor(Qt::BlankCursor, QCoreApplication::translate("QtCursorDatabase", "Blank"),
  72. QIcon());
  73. appendCursor(Qt::SplitVCursor, QCoreApplication::translate("QtCursorDatabase", "Split Vertical"),
  74. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png")));
  75. appendCursor(Qt::SplitHCursor, QCoreApplication::translate("QtCursorDatabase", "Split Horizontal"),
  76. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png")));
  77. appendCursor(Qt::PointingHandCursor, QCoreApplication::translate("QtCursorDatabase", "Pointing Hand"),
  78. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png")));
  79. appendCursor(Qt::ForbiddenCursor, QCoreApplication::translate("QtCursorDatabase", "Forbidden"),
  80. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png")));
  81. appendCursor(Qt::OpenHandCursor, QCoreApplication::translate("QtCursorDatabase", "Open Hand"),
  82. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png")));
  83. appendCursor(Qt::ClosedHandCursor, QCoreApplication::translate("QtCursorDatabase", "Closed Hand"),
  84. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png")));
  85. appendCursor(Qt::WhatsThisCursor, QCoreApplication::translate("QtCursorDatabase", "What's This"),
  86. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png")));
  87. appendCursor(Qt::BusyCursor, QCoreApplication::translate("QtCursorDatabase", "Busy"),
  88. QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png")));
  89. }
  90. void QtCursorDatabase::clear()
  91. {
  92. m_cursorNames.clear();
  93. m_cursorIcons.clear();
  94. m_valueToCursorShape.clear();
  95. m_cursorShapeToValue.clear();
  96. }
  97. void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
  98. {
  99. if (m_cursorShapeToValue.contains(shape))
  100. return;
  101. const int value = m_cursorNames.count();
  102. m_cursorNames.append(name);
  103. m_cursorIcons.insert(value, icon);
  104. m_valueToCursorShape.insert(value, shape);
  105. m_cursorShapeToValue.insert(shape, value);
  106. }
  107. QStringList QtCursorDatabase::cursorShapeNames() const
  108. {
  109. return m_cursorNames;
  110. }
  111. QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
  112. {
  113. return m_cursorIcons;
  114. }
  115. QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
  116. {
  117. int val = cursorToValue(cursor);
  118. if (val >= 0)
  119. return m_cursorNames.at(val);
  120. return QString();
  121. }
  122. QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
  123. {
  124. int val = cursorToValue(cursor);
  125. return m_cursorIcons.value(val);
  126. }
  127. int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
  128. {
  129. #ifndef QT_NO_CURSOR
  130. Qt::CursorShape shape = cursor.shape();
  131. if (m_cursorShapeToValue.contains(shape))
  132. return m_cursorShapeToValue[shape];
  133. #endif
  134. return -1;
  135. }
  136. #ifndef QT_NO_CURSOR
  137. QCursor QtCursorDatabase::valueToCursor(int value) const
  138. {
  139. if (m_valueToCursorShape.contains(value))
  140. return QCursor(m_valueToCursorShape[value]);
  141. return QCursor();
  142. }
  143. #endif
  144. QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
  145. {
  146. QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
  147. img.fill(0);
  148. QPainter painter(&img);
  149. painter.setCompositionMode(QPainter::CompositionMode_Source);
  150. painter.fillRect(0, 0, img.width(), img.height(), b);
  151. QColor color = b.color();
  152. if (color.alpha() != 255) { // indicate alpha by an inset
  153. QBrush opaqueBrush = b;
  154. color.setAlpha(255);
  155. opaqueBrush.setColor(color);
  156. painter.fillRect(img.width() / 4, img.height() / 4,
  157. img.width() / 2, img.height() / 2, opaqueBrush);
  158. }
  159. painter.end();
  160. return QPixmap::fromImage(img);
  161. }
  162. QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
  163. {
  164. return QIcon(brushValuePixmap(b));
  165. }
  166. QString QtPropertyBrowserUtils::colorValueText(const QColor &c)
  167. {
  168. return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)")
  169. .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
  170. }
  171. QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
  172. {
  173. QFont f = font;
  174. QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
  175. img.fill(0);
  176. QPainter p(&img);
  177. p.setRenderHint(QPainter::TextAntialiasing, true);
  178. p.setRenderHint(QPainter::Antialiasing, true);
  179. f.setPointSize(13);
  180. p.setFont(f);
  181. QTextOption t;
  182. t.setAlignment(Qt::AlignCenter);
  183. p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
  184. return QPixmap::fromImage(img);
  185. }
  186. QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
  187. {
  188. return QIcon(fontValuePixmap(f));
  189. }
  190. QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
  191. {
  192. return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2]")
  193. .arg(f.family()).arg(f.pointSize());
  194. }
  195. QString QtPropertyBrowserUtils::dateFormat()
  196. {
  197. QLocale loc;
  198. QString format = loc.dateFormat(QLocale::ShortFormat);
  199. // Change dd.MM.yy, MM/dd/yy to 4 digit years
  200. if (format.count(QLatin1Char('y')) == 2)
  201. format.insert(format.indexOf(QLatin1Char('y')), QLatin1String("yy"));
  202. return format;
  203. }
  204. QString QtPropertyBrowserUtils::timeFormat()
  205. {
  206. QLocale loc;
  207. // ShortFormat is missing seconds on UNIX.
  208. return loc.timeFormat(QLocale::LongFormat);
  209. }
  210. QString QtPropertyBrowserUtils::dateTimeFormat()
  211. {
  212. QString format = dateFormat();
  213. format += QLatin1Char(' ');
  214. format += timeFormat();
  215. return format;
  216. }
  217. QtBoolEdit::QtBoolEdit(QWidget *parent) :
  218. QWidget(parent),
  219. m_checkBox(new QCheckBox(this)),
  220. m_textVisible(true)
  221. {
  222. QHBoxLayout *lt = new QHBoxLayout;
  223. if (QApplication::layoutDirection() == Qt::LeftToRight)
  224. lt->setContentsMargins(4, 0, 0, 0);
  225. else
  226. lt->setContentsMargins(0, 0, 4, 0);
  227. lt->addWidget(m_checkBox);
  228. setLayout(lt);
  229. connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
  230. setFocusProxy(m_checkBox);
  231. m_checkBox->setText(tr("True"));
  232. }
  233. void QtBoolEdit::setTextVisible(bool textVisible)
  234. {
  235. if (m_textVisible == textVisible)
  236. return;
  237. m_textVisible = textVisible;
  238. if (m_textVisible)
  239. m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
  240. else
  241. m_checkBox->setText(QString());
  242. }
  243. Qt::CheckState QtBoolEdit::checkState() const
  244. {
  245. return m_checkBox->checkState();
  246. }
  247. void QtBoolEdit::setCheckState(Qt::CheckState state)
  248. {
  249. m_checkBox->setCheckState(state);
  250. }
  251. bool QtBoolEdit::isChecked() const
  252. {
  253. return m_checkBox->isChecked();
  254. }
  255. void QtBoolEdit::setChecked(bool c)
  256. {
  257. m_checkBox->setChecked(c);
  258. if (!m_textVisible)
  259. return;
  260. m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
  261. }
  262. bool QtBoolEdit::blockCheckBoxSignals(bool block)
  263. {
  264. return m_checkBox->blockSignals(block);
  265. }
  266. void QtBoolEdit::mousePressEvent(QMouseEvent *event)
  267. {
  268. if (event->buttons() == Qt::LeftButton) {
  269. m_checkBox->click();
  270. event->accept();
  271. } else {
  272. QWidget::mousePressEvent(event);
  273. }
  274. }
  275. QT_END_NAMESPACE