qpropertyeditor_delegate.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 398 行 · 第 1/2 页

CPP
398
字号
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the Qt Designer of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file.  Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include "qpropertyeditor_delegate_p.h"#include "qpropertyeditor_model_p.h"#include "textpropertyeditor_p.h"#include <iconloader_p.h>#include <QtGui/QPainter>#include <QtGui/QKeyEvent>#include <QtGui/QApplication>#include <QtGui/QToolButton>#include <QtGui/QHBoxLayout>#include <QtCore/qdebug.h>#include <private/qfont_p.h>namespace qdesigner_internal {class EditorWithReset : public QWidget{    Q_OBJECTpublic:    EditorWithReset(const IProperty *property, QPropertyEditorModel *model, QWidget *parent = 0);    void setChildEditor(QWidget *child_editor);    QWidget *childEditor() const { return m_child_editor; }private slots:    void emitResetProperty();signals:    void sync();    void resetProperty(const IProperty *property, QPropertyEditorModel *model);private:    QWidget *m_child_editor;    QHBoxLayout *m_layout;    const IProperty *m_property;    QPropertyEditorModel *m_model;};EditorWithReset::EditorWithReset(const IProperty *property, QPropertyEditorModel *model, QWidget *parent)    : QWidget(parent){    setAutoFillBackground(true);    m_property = property;    m_child_editor = 0;    m_layout = new QHBoxLayout(this);    m_layout->setMargin(0);    m_layout->setSpacing(0);    m_model = model;    QToolButton *button = new QToolButton(this);    button->setToolButtonStyle(Qt::ToolButtonIconOnly);    button->setIcon(createIconSet(QLatin1String("resetproperty.png")));    button->setIconSize(QSize(8,8));    button->setSizePolicy(QSizePolicy(QSizePolicy::Fixed, QSizePolicy::MinimumExpanding));    m_layout->addWidget(button);    connect(button, SIGNAL(clicked()), this, SLOT(emitResetProperty()));}void EditorWithReset::emitResetProperty(){    emit resetProperty(m_property, m_model);}void EditorWithReset::setChildEditor(QWidget *child_editor){    m_child_editor = child_editor;    m_child_editor->setSizePolicy(QSizePolicy(QSizePolicy::Expanding, QSizePolicy::MinimumExpanding));    m_layout->insertWidget(0, m_child_editor);    setFocusProxy(m_child_editor);}QPropertyEditorDelegate::QPropertyEditorDelegate(QObject *parent)    : QItemDelegate(parent),      m_readOnly(false),      m_syncing(false),      m_lastEdited(0){}QPropertyEditorDelegate::~QPropertyEditorDelegate(){}bool QPropertyEditorDelegate::eventFilter(QObject *object, QEvent *event){    QWidget *editor = qobject_cast<QWidget*>(object);    if (editor && qobject_cast<TextPropertyEditor*>(editor->parent()))        editor = editor->parentWidget();    if (editor && qobject_cast<EditorWithReset*>(editor->parent()))        editor = editor->parentWidget();    switch (event->type()) {        case QEvent::KeyPress:        case QEvent::KeyRelease: {            QKeyEvent *ke = static_cast<QKeyEvent*>(event);            if (!(ke->modifiers() & Qt::ControlModifier)                && (ke->key() == Qt::Key_Up || ke->key() == Qt::Key_Down)) {                event->ignore();                return true;            }            if (object->metaObject()->className() == QLatin1String("QtKeySequenceEdit")) {                event->ignore();                return false;            }        } break;        case QEvent::FocusOut:            if (!editor->isActiveWindow() || (QApplication::focusWidget() != editor)) {                QWidget *w = QApplication::focusWidget();                while (w) { // do not worry about focus changes internally in the editor                    if (w == editor)                        return false;                    w = w->parentWidget();                }                emit commitData(editor);            }            return false;        default:            break;    }    return QItemDelegate::eventFilter(editor ? editor : object, event);}void QPropertyEditorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const{    QStyleOptionViewItem option = opt;    const QAbstractItemModel *model = index.model();    IProperty *property = static_cast<const QPropertyEditorModel*>(model)->privateData(index);    if (index.column() == 0 && property && property->changed()) {        option.font.setBold(true);    }    if (property && property->isSeparator()) {        option.palette.setColor(QPalette::Text, option.palette.color(QPalette::BrightText));        option.font.setBold(true);        option.state &= ~QStyle::State_Selected;    }    if (index.column() == 1) {        option.state &= ~QStyle::State_Selected;    }    option.state &= ~QStyle::State_HasFocus;    if (property && property->isSeparator()) {        const QBrush bg = option.palette.dark();        painter->fillRect(option.rect, bg);    }    const QPen savedPen = painter->pen();    QItemDelegate::paint(painter, option, index);    const QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &option));    painter->setPen(QPen(color));    if (index.column() == 1 || !(property && property->isSeparator())) {        const int right = (option.direction == Qt::LeftToRight) ? option.rect.right() : option.rect.left();        painter->drawLine(right, option.rect.y(), right, option.rect.bottom());    }    painter->drawLine(option.rect.x(), option.rect.bottom(),            option.rect.right(), option.rect.bottom());

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?