📄 paletteeditor.cpp
字号:
case QPalette::ButtonText: case QPalette::Base: break; case QPalette::Dark: m_palette.setBrush(QPalette::Disabled, QPalette::Foreground, c); m_palette.setBrush(QPalette::Disabled, QPalette::Dark, c); m_palette.setBrush(QPalette::Disabled, QPalette::Text, c); m_palette.setBrush(QPalette::Disabled, QPalette::ButtonText, c); idxBegin = PaletteModel::index(0, 0); idxEnd = PaletteModel::index(m_roleNames.count() - 1, 3); break; case QPalette::Background: m_palette.setBrush(QPalette::Disabled, QPalette::Base, c); m_palette.setBrush(QPalette::Disabled, QPalette::Background, c); idxBegin = PaletteModel::index(QPalette::Base, 0); break; case QPalette::Highlight: m_palette.setBrush(QPalette::Disabled, QPalette::Highlight, c.dark(120)); break; default: m_palette.setBrush(QPalette::Disabled, r, c); break; } } emit paletteChanged(m_palette); emit dataChanged(idxBegin, idxEnd); return true; } if (index.column() == 0 && role == Qt::EditRole) { uint mask = m_palette.resolve(); bool isMask = qVariantValue<bool>(value); int r = index.row(); if (isMask) mask |= (1 << r); else { m_palette.setBrush(QPalette::Active, (QPalette::ColorRole)r, m_parentPalette.brush(QPalette::Active, (QPalette::ColorRole)r)); m_palette.setBrush(QPalette::Inactive, (QPalette::ColorRole)r, m_parentPalette.brush(QPalette::Inactive, (QPalette::ColorRole)r)); m_palette.setBrush(QPalette::Disabled, (QPalette::ColorRole)r, m_parentPalette.brush(QPalette::Disabled, (QPalette::ColorRole)r)); mask &= ~(1 << index.row()); } m_palette.resolve(mask); emit paletteChanged(m_palette); QModelIndex idxEnd = PaletteModel::index(r, 3); emit dataChanged(index, idxEnd); return true; } return false;}Qt::ItemFlags PaletteModel::flags(const QModelIndex &index) const{ if (!index.isValid()) return Qt::ItemIsEnabled; return Qt::ItemIsEditable | Qt::ItemIsEnabled;}QVariant PaletteModel::headerData(int section, Qt::Orientation orientation, int role) const{ if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { if (section == 0) return tr("Color Role"); if (section == groupToColumn(QPalette::Active)) return tr("Active"); if (section == groupToColumn(QPalette::Inactive)) return tr("Inactive"); if (section == groupToColumn(QPalette::Disabled)) return tr("Disabled"); } return QVariant();}QPalette PaletteModel::getPalette() const{ return m_palette;}void PaletteModel::setPalette(const QPalette &palette, const QPalette &parentPalette){ m_parentPalette = parentPalette; m_palette = palette; QModelIndex idxBegin = index(0, 0); QModelIndex idxEnd = index(m_roleNames.count() - 1, 3); emit dataChanged(idxBegin, idxEnd);}QPalette::ColorGroup PaletteModel::columnToGroup(int index) const{ if (index == 1) return QPalette::Active; if (index == 2) return QPalette::Inactive; return QPalette::Disabled;}int PaletteModel::groupToColumn(QPalette::ColorGroup group) const{ if (group == QPalette::Active) return 1; if (group == QPalette::Inactive) return 2; return 3;}//////////////////////////ColorEditor::ColorEditor(QWidget *parent) : QWidget(parent){ QLayout *layout = new QHBoxLayout(this); layout->setMargin(0); button = new StyledButton(this); layout->addWidget(button); connect(button, SIGNAL(changed()), this, SLOT(colorChanged())); setFocusProxy(button); m_changed = false;}void ColorEditor::setColor(const QColor &color){ button->setBrush(color); m_changed = false;}QColor ColorEditor::color() const{ return button->brush().color();}void ColorEditor::colorChanged(){ m_changed = true; emit changed(this);}bool ColorEditor::changed() const{ return m_changed;}//////////////////////////RoleEditor::RoleEditor(QWidget *parent) : QWidget(parent){ m_edited = false; QHBoxLayout *layout = new QHBoxLayout(this); layout->setMargin(0); layout->setSpacing(0); m_label = new QLabel(this); layout->addWidget(m_label); m_label->setAutoFillBackground(true); m_label->setIndent(3); // ### hardcode it should have the same value of textMargin in QItemDelegate setFocusProxy(m_label); 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)); layout->addWidget(button); connect(button, SIGNAL(clicked()), this, SLOT(emitResetProperty()));}void RoleEditor::setLabel(const QString &label){ m_label->setText(label);}void RoleEditor::setEdited(bool on){ QFont font; if (on == true) { font.setBold(on); } m_label->setFont(font); m_edited = on;}bool RoleEditor::edited() const{ return m_edited;}void RoleEditor::emitResetProperty(){ setEdited(false); emit changed(this);}QWidget *ColorDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const{ QWidget *ed = 0; if (index.column() == 0) { RoleEditor *editor = new RoleEditor(parent); connect(editor, SIGNAL(changed(QWidget *)), this, SIGNAL(commitData(QWidget *))); //editor->setFocusPolicy(Qt::NoFocus); //editor->installEventFilter(const_cast<ColorDelegate *>(this)); ed = editor; } else { ColorEditor *editor = new ColorEditor(parent); connect(editor, SIGNAL(changed(QWidget *)), this, SIGNAL(commitData(QWidget *))); editor->setFocusPolicy(Qt::NoFocus); editor->installEventFilter(const_cast<ColorDelegate *>(this)); ed = editor; } return ed;}void ColorDelegate::setEditorData(QWidget *ed, const QModelIndex &index) const{ if (index.column() == 0) { bool mask = qVariantValue<bool>(index.model()->data(index, Qt::EditRole)); RoleEditor *editor = static_cast<RoleEditor *>(ed); editor->setEdited(mask); QString colorName = qVariantValue<QString>(index.model()->data(index, Qt::DisplayRole)); editor->setLabel(colorName); } else { QColor c = qVariantValue<QColor>(index.model()->data(index, Qt::BackgroundColorRole)); ColorEditor *editor = static_cast<ColorEditor *>(ed); editor->setColor(c); }}void ColorDelegate::setModelData(QWidget *ed, QAbstractItemModel *model, const QModelIndex &index) const{ if (index.column() == 0) { RoleEditor *editor = static_cast<RoleEditor *>(ed); bool mask = editor->edited(); model->setData(index, mask, Qt::EditRole); } else { ColorEditor *editor = static_cast<ColorEditor *>(ed); if (editor->changed()) { QColor c = editor->color(); model->setData(index, c, Qt::BackgroundColorRole); } }}void ColorDelegate::updateEditorGeometry(QWidget *ed, const QStyleOptionViewItem &option, const QModelIndex &index) const{ QItemDelegate::updateEditorGeometry(ed, option, index); ed->setGeometry(ed->geometry().adjusted(0, 0, -1, -1));}void ColorDelegate::paint(QPainter *painter, const QStyleOptionViewItem &opt, const QModelIndex &index) const{ QStyleOptionViewItem option = opt; bool mask = qVariantValue<bool>(index.model()->data(index, Qt::EditRole)); if (index.column() == 0 && mask) { option.font.setBold(true); } QItemDelegate::paint(painter, option, index); painter->drawLine(option.rect.right(), option.rect.y(), option.rect.right(), option.rect.bottom()); painter->drawLine(option.rect.x(), option.rect.bottom(), option.rect.right(), option.rect.bottom());}QSize ColorDelegate::sizeHint(const QStyleOptionViewItem &opt, const QModelIndex &index) const{ return QItemDelegate::sizeHint(opt, index) + QSize(4, 4);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -