paletteeditor.cpp

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

CPP
638
字号
/******************************************************************************** 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.******************************************************************************//*TRANSLATOR qdesigner_internal::PaletteEditor*//*TRANSLATOR qdesigner_internal::PaletteModel*/#include "paletteeditor.h"#include "qtcolorbutton.h"#include "findicondialog_p.h"#include <iconloader_p.h>#include <QtDesigner/QDesignerFormEditorInterface>#include <QtDesigner/QDesignerFormWindowManagerInterface>#include <QtDesigner/QDesignerIconCacheInterface>#include <QtCore/QMetaProperty>#include <QtGui/QPainter>#include <QtGui/QToolButton>#include <QtGui/QLabel>#include <QtGui/QHeaderView>namespace qdesigner_internal {enum { BrushRole = 33 };PaletteEditor::PaletteEditor(QDesignerFormEditorInterface *core, QWidget *parent) :     QDialog(parent),    m_currentColorGroup(QPalette::Active),    m_paletteModel(new PaletteModel(this)),    m_modelUpdated(false),    m_paletteUpdated(false),    m_compute(true),    m_core(core){    ui.setupUi(this);    ui.paletteView->setModel(m_paletteModel);    updatePreviewPalette();    updateStyledButton();    ui.paletteView->setModel(m_paletteModel);    ColorDelegate *delegate = new ColorDelegate(core, this);    ui.paletteView->setItemDelegate(delegate);    ui.paletteView->setEditTriggers(QAbstractItemView::AllEditTriggers);    connect(m_paletteModel, SIGNAL(paletteChanged(const QPalette &)),                this, SLOT(paletteChanged(const QPalette &)));    ui.paletteView->setSelectionBehavior(QAbstractItemView::SelectRows);    ui.paletteView->setDragEnabled(true);    ui.paletteView->setDropIndicatorShown(true);    ui.paletteView->setRootIsDecorated(false);    ui.paletteView->setColumnHidden(2, true);    ui.paletteView->setColumnHidden(3, true);}PaletteEditor::~PaletteEditor(){}QPalette PaletteEditor::palette() const{    return m_editPalette;}void PaletteEditor::setPalette(const QPalette &palette){    m_editPalette = palette;    const uint mask = palette.resolve();    for (int i = 0; i < (int)QPalette::NColorRoles; i++) {        if (!(mask & (1 << i))) {            m_editPalette.setBrush(QPalette::Active, static_cast<QPalette::ColorRole>(i),                        m_parentPalette.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));            m_editPalette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i),                        m_parentPalette.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));            m_editPalette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i),                        m_parentPalette.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));        }    }    m_editPalette.resolve(mask);    updatePreviewPalette();    updateStyledButton();    m_paletteUpdated = true;    if (!m_modelUpdated)        m_paletteModel->setPalette(m_editPalette, m_parentPalette);    m_paletteUpdated = false;}void PaletteEditor::setPalette(const QPalette &palette, const QPalette &parentPalette){    m_parentPalette = parentPalette;    setPalette(palette);}void PaletteEditor::on_buildButton_colorChanged(const QColor &){    buildPalette();}void PaletteEditor::on_activeRadio_clicked(){    m_currentColorGroup = QPalette::Active;    updatePreviewPalette();}void PaletteEditor::on_inactiveRadio_clicked(){    m_currentColorGroup = QPalette::Inactive;    updatePreviewPalette();}void PaletteEditor::on_disabledRadio_clicked(){    m_currentColorGroup = QPalette::Disabled;    updatePreviewPalette();}void PaletteEditor::on_computeRadio_clicked(){    if (m_compute)        return;    ui.paletteView->setColumnHidden(2, true);    ui.paletteView->setColumnHidden(3, true);    m_compute = true;    m_paletteModel->setCompute(true);}void PaletteEditor::on_detailsRadio_clicked(){    if (!m_compute)        return;    const int w = ui.paletteView->columnWidth(1);    ui.paletteView->setColumnHidden(2, false);    ui.paletteView->setColumnHidden(3, false);    QHeaderView *header = ui.paletteView->header();    header->resizeSection(1, w / 3);    header->resizeSection(2, w / 3);    header->resizeSection(3, w / 3);    m_compute = false;    m_paletteModel->setCompute(false);}void PaletteEditor::paletteChanged(const QPalette &palette){    m_modelUpdated = true;    if (!m_paletteUpdated)        setPalette(palette);    m_modelUpdated = false;}void PaletteEditor::buildPalette(){    const QColor btn = ui.buildButton->color();    const QPalette temp = QPalette(btn);    setPalette(temp);}void PaletteEditor::updatePreviewPalette(){    const QPalette::ColorGroup g = currentColorGroup();    // build the preview palette    const QPalette currentPalette = palette();    QPalette previewPalette;    for (int i = QPalette::WindowText; i < QPalette::NColorRoles; i++) {        const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(i);        const QBrush br = currentPalette.brush(g, r);        previewPalette.setBrush(QPalette::Active, r, br);        previewPalette.setBrush(QPalette::Inactive, r, br);        previewPalette.setBrush(QPalette::Disabled, r, br);    }    ui.previewFrame->setPreviewPalette(previewPalette);    const bool enabled = g != QPalette::Disabled;    ui.previewFrame->setEnabled(enabled);    ui.previewFrame->setSubWindowActive(g != QPalette::Inactive);}void PaletteEditor::updateStyledButton(){    ui.buildButton->setColor(palette().color(QPalette::Active, QPalette::Button));}QPalette PaletteEditor::getPalette(QDesignerFormEditorInterface *core, QWidget* parent, const QPalette &init,            const QPalette &parentPal, int *ok){    PaletteEditor dlg(core, parent);    QPalette parentPalette(parentPal);    uint mask = init.resolve();    for (int i = 0; i < (int)QPalette::NColorRoles; i++) {        if (!(mask & (1 << i))) {            parentPalette.setBrush(QPalette::Active, static_cast<QPalette::ColorRole>(i),                        init.brush(QPalette::Active, static_cast<QPalette::ColorRole>(i)));            parentPalette.setBrush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i),                        init.brush(QPalette::Inactive, static_cast<QPalette::ColorRole>(i)));            parentPalette.setBrush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i),                        init.brush(QPalette::Disabled, static_cast<QPalette::ColorRole>(i)));        }    }    dlg.setPalette(init, parentPalette);    const int result = dlg.exec();    if (ok) *ok = result;    return result == QDialog::Accepted ? dlg.palette() : init;}//////////////////////PaletteModel::PaletteModel(QObject *parent)  :     QAbstractTableModel(parent),    m_compute(true){    const QMetaObject *meta = metaObject();    const int index = meta->indexOfProperty("colorRole");    const QMetaProperty p = meta->property(index);    const QMetaEnum e = p.enumerator();    for (int r = QPalette::WindowText; r < QPalette::NColorRoles; r++) {        m_roleNames[static_cast<QPalette::ColorRole>(r)] = QLatin1String(e.key(r));    }}int PaletteModel::rowCount(const QModelIndex &) const{    return m_roleNames.count();}int PaletteModel::columnCount(const QModelIndex &) const{    return 4;}QVariant PaletteModel::data(const QModelIndex &index, int role) const{    if (!index.isValid())        return QVariant();    if (index.row() < 0 || index.row() >= QPalette::NColorRoles)        return QVariant();    if (index.column() < 0 || index.column() >= 4)        return QVariant();    if (index.column() == 0) {        if (role == Qt::DisplayRole)            return m_roleNames[static_cast<QPalette::ColorRole>(index.row())];        if (role == Qt::EditRole) {            const uint mask = m_palette.resolve();            if (mask & (1 << index.row()))                return true;            return false;        }        return QVariant();    }    if (role == BrushRole)        return m_palette.brush(columnToGroup(index.column()),                    static_cast<QPalette::ColorRole>(index.row()));    return QVariant();}bool PaletteModel::setData(const QModelIndex &index, const QVariant &value, int role){    if (!index.isValid())        return false;    if (index.column() != 0 && role == BrushRole) {        const QBrush br = qVariantValue<QBrush>(value);        const QPalette::ColorRole r = static_cast<QPalette::ColorRole>(index.row());        const QPalette::ColorGroup g = columnToGroup(index.column());        m_palette.setBrush(g, r, br);        QModelIndex idxBegin = PaletteModel::index(r, 0);        QModelIndex idxEnd = PaletteModel::index(r, 3);        if (m_compute) {            m_palette.setBrush(QPalette::Inactive, r, br);            switch (r) {                case QPalette::WindowText:                case QPalette::Text:                case QPalette::ButtonText:                case QPalette::Base:                    break;                case QPalette::Dark:                    m_palette.setBrush(QPalette::Disabled, QPalette::WindowText, br);                    m_palette.setBrush(QPalette::Disabled, QPalette::Dark, br);                    m_palette.setBrush(QPalette::Disabled, QPalette::Text, br);                    m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, br);                    idxBegin = PaletteModel::index(0, 0);                    idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3);

⌨️ 快捷键说明

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