File indexing completed on 2024-05-12 04:35:10

0001 /****************************************************************************
0002 **
0003 ** Copyright (C) 2016 The Qt Company Ltd.
0004 ** Contact: https://www.qt.io/licensing/
0005 **
0006 ** This file is part of the tools applications of the Qt Toolkit.
0007 **
0008 ** $QT_BEGIN_LICENSE:LGPL$
0009 ** Commercial License Usage
0010 ** Licensees holding valid commercial Qt licenses may use this file in
0011 ** accordance with the commercial license agreement provided with the
0012 ** Software or, alternatively, in accordance with the terms contained in
0013 ** a written agreement between you and The Qt Company. For licensing terms
0014 ** and conditions see https://www.qt.io/terms-conditions. For further
0015 ** information use the contact form at https://www.qt.io/contact-us.
0016 **
0017 ** GNU Lesser General Public License Usage
0018 ** Alternatively, this file may be used under the terms of the GNU Lesser
0019 ** General Public License version 3 as published by the Free Software
0020 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
0021 ** packaging of this file. Please review the following information to
0022 ** ensure the GNU Lesser General Public License version 3 requirements
0023 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
0024 **
0025 ** GNU General Public License Usage
0026 ** Alternatively, this file may be used under the terms of the GNU
0027 ** General Public License version 2.0 or (at your option) the GNU General
0028 ** Public license version 3 or any later version approved by the KDE Free
0029 ** Qt Foundation. The licenses are as published by the Free Software
0030 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
0031 ** included in the packaging of this file. Please review the following
0032 ** information to ensure the GNU General Public License requirements will
0033 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
0034 ** https://www.gnu.org/licenses/gpl-3.0.html.
0035 **
0036 ** $QT_END_LICENSE$
0037 **
0038 ****************************************************************************/
0039 
0040 #include "qtpropertybrowserutils_p.h"
0041 #include <QtWidgets/QApplication>
0042 #include <QtGui/QPainter>
0043 #include <QtWidgets/QHBoxLayout>
0044 #include <QtGui/QMouseEvent>
0045 #include <QtWidgets/QCheckBox>
0046 #include <QtWidgets/QLineEdit>
0047 #include <QtWidgets/QMenu>
0048 #include <QtCore/QLocale>
0049 
0050 QT_BEGIN_NAMESPACE
0051 
0052 QtCursorDatabase::QtCursorDatabase()
0053 {
0054     appendCursor(Qt::ArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Arrow"),
0055                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-arrow.png")));
0056     appendCursor(Qt::UpArrowCursor, QCoreApplication::translate("QtCursorDatabase", "Up Arrow"),
0057                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-uparrow.png")));
0058     appendCursor(Qt::CrossCursor, QCoreApplication::translate("QtCursorDatabase", "Cross"),
0059                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-cross.png")));
0060     appendCursor(Qt::WaitCursor, QCoreApplication::translate("QtCursorDatabase", "Wait"),
0061                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-wait.png")));
0062     appendCursor(Qt::IBeamCursor, QCoreApplication::translate("QtCursorDatabase", "IBeam"),
0063                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-ibeam.png")));
0064     appendCursor(Qt::SizeVerCursor, QCoreApplication::translate("QtCursorDatabase", "Size Vertical"),
0065                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizev.png")));
0066     appendCursor(Qt::SizeHorCursor, QCoreApplication::translate("QtCursorDatabase", "Size Horizontal"),
0067                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeh.png")));
0068     appendCursor(Qt::SizeFDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Backslash"),
0069                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizef.png")));
0070     appendCursor(Qt::SizeBDiagCursor, QCoreApplication::translate("QtCursorDatabase", "Size Slash"),
0071                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeb.png")));
0072     appendCursor(Qt::SizeAllCursor, QCoreApplication::translate("QtCursorDatabase", "Size All"),
0073                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-sizeall.png")));
0074     appendCursor(Qt::BlankCursor, QCoreApplication::translate("QtCursorDatabase", "Blank"),
0075                  QIcon());
0076     appendCursor(Qt::SplitVCursor, QCoreApplication::translate("QtCursorDatabase", "Split Vertical"),
0077                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-vsplit.png")));
0078     appendCursor(Qt::SplitHCursor, QCoreApplication::translate("QtCursorDatabase", "Split Horizontal"),
0079                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hsplit.png")));
0080     appendCursor(Qt::PointingHandCursor, QCoreApplication::translate("QtCursorDatabase", "Pointing Hand"),
0081                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-hand.png")));
0082     appendCursor(Qt::ForbiddenCursor, QCoreApplication::translate("QtCursorDatabase", "Forbidden"),
0083                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-forbidden.png")));
0084     appendCursor(Qt::OpenHandCursor, QCoreApplication::translate("QtCursorDatabase", "Open Hand"),
0085                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-openhand.png")));
0086     appendCursor(Qt::ClosedHandCursor, QCoreApplication::translate("QtCursorDatabase", "Closed Hand"),
0087                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-closedhand.png")));
0088     appendCursor(Qt::WhatsThisCursor, QCoreApplication::translate("QtCursorDatabase", "What's This"),
0089                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-whatsthis.png")));
0090     appendCursor(Qt::BusyCursor, QCoreApplication::translate("QtCursorDatabase", "Busy"),
0091                  QIcon(QLatin1String(":/qt-project.org/qtpropertybrowser/images/cursor-busy.png")));
0092 }
0093 
0094 void QtCursorDatabase::clear()
0095 {
0096     m_cursorNames.clear();
0097     m_cursorIcons.clear();
0098     m_valueToCursorShape.clear();
0099     m_cursorShapeToValue.clear();
0100 }
0101 
0102 void QtCursorDatabase::appendCursor(Qt::CursorShape shape, const QString &name, const QIcon &icon)
0103 {
0104     if (m_cursorShapeToValue.contains(shape))
0105         return;
0106     const int value = m_cursorNames.count();
0107     m_cursorNames.append(name);
0108     m_cursorIcons.insert(value, icon);
0109     m_valueToCursorShape.insert(value, shape);
0110     m_cursorShapeToValue.insert(shape, value);
0111 }
0112 
0113 QStringList QtCursorDatabase::cursorShapeNames() const
0114 {
0115     return m_cursorNames;
0116 }
0117 
0118 QMap<int, QIcon> QtCursorDatabase::cursorShapeIcons() const
0119 {
0120     return m_cursorIcons;
0121 }
0122 
0123 QString QtCursorDatabase::cursorToShapeName(const QCursor &cursor) const
0124 {
0125     int val = cursorToValue(cursor);
0126     if (val >= 0)
0127         return m_cursorNames.at(val);
0128     return QString();
0129 }
0130 
0131 QIcon QtCursorDatabase::cursorToShapeIcon(const QCursor &cursor) const
0132 {
0133     int val = cursorToValue(cursor);
0134     return m_cursorIcons.value(val);
0135 }
0136 
0137 int QtCursorDatabase::cursorToValue(const QCursor &cursor) const
0138 {
0139 #ifndef QT_NO_CURSOR
0140     Qt::CursorShape shape = cursor.shape();
0141     if (m_cursorShapeToValue.contains(shape))
0142         return m_cursorShapeToValue[shape];
0143 #endif
0144     return -1;
0145 }
0146 
0147 #ifndef QT_NO_CURSOR
0148 QCursor QtCursorDatabase::valueToCursor(int value) const
0149 {
0150     if (m_valueToCursorShape.contains(value))
0151         return QCursor(m_valueToCursorShape[value]);
0152     return QCursor();
0153 }
0154 #endif
0155 
0156 QPixmap QtPropertyBrowserUtils::brushValuePixmap(const QBrush &b)
0157 {
0158     QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
0159     img.fill(0);
0160 
0161     QPainter painter(&img);
0162     painter.setCompositionMode(QPainter::CompositionMode_Source);
0163     painter.fillRect(0, 0, img.width(), img.height(), b);
0164     QColor color = b.color();
0165     if (color.alpha() != 255) { // indicate alpha by an inset
0166         QBrush  opaqueBrush = b;
0167         color.setAlpha(255);
0168         opaqueBrush.setColor(color);
0169         painter.fillRect(img.width() / 4, img.height() / 4,
0170                          img.width() / 2, img.height() / 2, opaqueBrush);
0171     }
0172     painter.end();
0173     return QPixmap::fromImage(img);
0174 }
0175 
0176 QIcon QtPropertyBrowserUtils::brushValueIcon(const QBrush &b)
0177 {
0178     return QIcon(brushValuePixmap(b));
0179 }
0180 
0181 QString QtPropertyBrowserUtils::colorValueText(const QColor &c)
0182 {
0183     return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2, %3] (%4)")
0184            .arg(c.red()).arg(c.green()).arg(c.blue()).arg(c.alpha());
0185 }
0186 
0187 QPixmap QtPropertyBrowserUtils::fontValuePixmap(const QFont &font)
0188 {
0189     QFont f = font;
0190     QImage img(16, 16, QImage::Format_ARGB32_Premultiplied);
0191     img.fill(0);
0192     QPainter p(&img);
0193     p.setRenderHint(QPainter::TextAntialiasing, true);
0194     p.setRenderHint(QPainter::Antialiasing, true);
0195     f.setPointSize(13);
0196     p.setFont(f);
0197     QTextOption t;
0198     t.setAlignment(Qt::AlignCenter);
0199     p.drawText(QRect(0, 0, 16, 16), QString(QLatin1Char('A')), t);
0200     return QPixmap::fromImage(img);
0201 }
0202 
0203 QIcon QtPropertyBrowserUtils::fontValueIcon(const QFont &f)
0204 {
0205     return QIcon(fontValuePixmap(f));
0206 }
0207 
0208 QString QtPropertyBrowserUtils::fontValueText(const QFont &f)
0209 {
0210     return QCoreApplication::translate("QtPropertyBrowserUtils", "[%1, %2]")
0211            .arg(f.family()).arg(f.pointSize());
0212 }
0213 
0214 QString QtPropertyBrowserUtils::dateFormat()
0215 {
0216     QLocale loc;
0217     return loc.dateFormat(QLocale::ShortFormat);
0218 }
0219 
0220 QString QtPropertyBrowserUtils::timeFormat()
0221 {
0222     QLocale loc;
0223     // ShortFormat is missing seconds on UNIX.
0224     return loc.timeFormat(QLocale::LongFormat);
0225 }
0226 
0227 QString QtPropertyBrowserUtils::dateTimeFormat()
0228 {
0229     QString format = dateFormat();
0230     format += QLatin1Char(' ');
0231     format += timeFormat();
0232     return format;
0233 }
0234 
0235 QtBoolEdit::QtBoolEdit(QWidget *parent) :
0236     QWidget(parent),
0237     m_checkBox(new QCheckBox(this)),
0238     m_textVisible(true)
0239 {
0240     QHBoxLayout *lt = new QHBoxLayout;
0241     if (QApplication::layoutDirection() == Qt::LeftToRight)
0242         lt->setContentsMargins(4, 0, 0, 0);
0243     else
0244         lt->setContentsMargins(0, 0, 4, 0);
0245     lt->addWidget(m_checkBox);
0246     setLayout(lt);
0247     connect(m_checkBox, SIGNAL(toggled(bool)), this, SIGNAL(toggled(bool)));
0248     setFocusProxy(m_checkBox);
0249     m_checkBox->setText(tr("True"));
0250 }
0251 
0252 void QtBoolEdit::setTextVisible(bool textVisible)
0253 {
0254     if (m_textVisible == textVisible)
0255         return;
0256 
0257     m_textVisible = textVisible;
0258     if (m_textVisible)
0259         m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
0260     else
0261         m_checkBox->setText(QString());
0262 }
0263 
0264 Qt::CheckState QtBoolEdit::checkState() const
0265 {
0266     return m_checkBox->checkState();
0267 }
0268 
0269 void QtBoolEdit::setCheckState(Qt::CheckState state)
0270 {
0271     m_checkBox->setCheckState(state);
0272 }
0273 
0274 bool QtBoolEdit::isChecked() const
0275 {
0276     return m_checkBox->isChecked();
0277 }
0278 
0279 void QtBoolEdit::setChecked(bool c)
0280 {
0281     m_checkBox->setChecked(c);
0282     if (!m_textVisible)
0283         return;
0284     m_checkBox->setText(isChecked() ? tr("True") : tr("False"));
0285 }
0286 
0287 bool QtBoolEdit::blockCheckBoxSignals(bool block)
0288 {
0289     return m_checkBox->blockSignals(block);
0290 }
0291 
0292 void QtBoolEdit::mousePressEvent(QMouseEvent *event)
0293 {
0294     if (event->buttons() == Qt::LeftButton) {
0295         m_checkBox->click();
0296         event->accept();
0297     } else {
0298         QWidget::mousePressEvent(event);
0299     }
0300 }
0301 
0302 QT_END_NAMESPACE