qpropertyeditor.cpp
来自「QT 开发环境里面一个很重要的文件」· C++ 代码 · 共 166 行
CPP
166 行
/******************************************************************************** Copyright (C) 1992-2006 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://www.trolltech.com/products/qt/opensource.html**** If you are unsure which license is appropriate for your use, please** review the following information:** http://www.trolltech.com/products/qt/licensing.html or contact the** sales department at sales@trolltech.com.**** 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.h"#include "qpropertyeditor_model_p.h"#include "qpropertyeditor_delegate_p.h"#include <QtGui/QHeaderView>#include <QtGui/QApplication>#include <QtGui/QPainter>#include <QtGui/QScrollBar>#include <qdebug.h>using namespace qdesigner_internal;Q_GLOBAL_STATIC_WITH_ARGS(PropertyCollection, dummy_collection, (QLatin1String("<empty>")))QPropertyEditor::QPropertyEditor(QWidget *parent) : QTreeView(parent), contentsResized(false){ m_model = new QPropertyEditorModel(this); setModel(m_model); m_itemDelegate = new QPropertyEditorDelegate(this); setItemDelegate(m_itemDelegate); connect(m_itemDelegate, SIGNAL(resetProperty(const QString &)), m_model, SIGNAL(resetProperty(const QString &))); setInitialInput(0); setAlternatingRowColors(true); setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOn); connect(this, SIGNAL(activated(QModelIndex)), this, SLOT(expand(QModelIndex))); connect(m_model, SIGNAL(propertyChanged(IProperty*)), this, SIGNAL(propertyChanged(IProperty*)));}QPropertyEditor::~QPropertyEditor(){}bool QPropertyEditor::isReadOnly() const{ return m_itemDelegate->isReadOnly();}void QPropertyEditor::setReadOnly(bool readOnly){ m_itemDelegate->setReadOnly(readOnly);}void QPropertyEditor::setInitialInput(IProperty *initialInput){ bool needResize = false; if (!m_model->initialInput() || m_model->initialInput() == dummy_collection()) { if (initialInput) needResize = true; } if (!initialInput) initialInput = dummy_collection(); m_model->setInitialInput(initialInput); setSelectionMode(QTreeView::SingleSelection); setSelectionBehavior(QTreeView::SelectRows); setRootIsDecorated(true); setEditTriggers(QAbstractItemView::CurrentChanged|QAbstractItemView::SelectedClicked); setRootIndex(m_model->indexOf(initialInput)); if (needResize && !contentsResized) { contentsResized = true; resizeColumnToContents(0); }}IProperty *QPropertyEditor::initialInput() const{ return m_model->initialInput();}void QPropertyEditor::drawBranches(QPainter *painter, const QRect &rect, const QModelIndex &index) const{ // designer figts the style it uses. :( static bool mac_style = QApplication::style()->inherits("QMacStyle"); static const int windows_deco_size = 9; QStyleOptionViewItem opt = viewOptions(); IProperty *property = static_cast<const QPropertyEditorModel*>(model())->privateData(index); if (index.column() == 0 && property && property->changed()) { opt.font.setBold(true); } if (property && property->isSeparator()) { painter->fillRect(rect, opt.palette.dark()); } if (model()->hasChildren(index)) { opt.state |= QStyle::State_Children; QRect primitive(rect.left(), rect.top(), indentation(), rect.height()); if (!mac_style) { primitive.moveLeft(primitive.left() + (primitive.width() - windows_deco_size)/2); primitive.moveTop(primitive.top() + (primitive.height() - windows_deco_size)/2); primitive.setWidth(windows_deco_size); primitive.setHeight(windows_deco_size); } opt.rect = primitive; if (isExpanded(index)) opt.state |= QStyle::State_Open; style()->drawPrimitive(QStyle::PE_IndicatorBranch, &opt, painter, this); } QPen savedPen = painter->pen(); QColor color = static_cast<QRgb>(QApplication::style()->styleHint(QStyle::SH_Table_GridLineColor, &opt)); painter->setPen(QPen(color)); painter->drawLine(rect.x(), rect.bottom(), rect.right(), rect.bottom()); painter->setPen(savedPen);}void QPropertyEditor::keyPressEvent(QKeyEvent *ev){/* QApplication::syncX();*/ QTreeView::keyPressEvent(ev);}QStyleOptionViewItem QPropertyEditor::viewOptions() const{ QStyleOptionViewItem option = QTreeView::viewOptions(); option.showDecorationSelected = true; return option;}void QPropertyEditor::focusInEvent(QFocusEvent *event){ QAbstractScrollArea::focusInEvent(event); viewport()->update();}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?