qpropertyeditor_items.cpp

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

CPP
2,138
字号
    }}// QtKeqSequenceEditclass QtKeySequenceEdit : public QWidget{    Q_OBJECTpublic:    QtKeySequenceEdit(QWidget *parent = 0);    QKeySequence keySequence() const;    bool eventFilter(QObject *o, QEvent *e);public Q_SLOTS:    void setKeySequence(const QKeySequence &sequence);Q_SIGNALS:    void keySequenceChanged(const QKeySequence &sequence);protected:    void focusInEvent(QFocusEvent *e);    void focusOutEvent(QFocusEvent *e);    void keyPressEvent(QKeyEvent *e);    void keyReleaseEvent(QKeyEvent *e);    bool event(QEvent *e);private slots:    void slotClearShortcut();private:    void handleKeyEvent(QKeyEvent *e);    int translateModifiers(Qt::KeyboardModifiers state) const;    int m_num;    QKeySequence m_keySequence;    QLineEdit *m_lineEdit;};bool QtKeySequenceEdit::event(QEvent *e){    if (e->type() == QEvent::Shortcut ||            e->type() == QEvent::ShortcutOverride  ||            e->type() == QEvent::KeyRelease) {        e->accept();        return true;    }    return QWidget::event(e);}QtKeySequenceEdit::QtKeySequenceEdit(QWidget *parent)    : QWidget(parent), m_num(0){    m_lineEdit = new QLineEdit(this);    QHBoxLayout *layout = new QHBoxLayout(this);    layout->addWidget(m_lineEdit);    layout->setMargin(0);    m_lineEdit->installEventFilter(this);    m_lineEdit->setReadOnly(true);    m_lineEdit->setFocusProxy(this);    setFocusPolicy(m_lineEdit->focusPolicy());    setAttribute(Qt::WA_InputMethodEnabled);}bool QtKeySequenceEdit::eventFilter(QObject *o, QEvent *e){    if (o == m_lineEdit && e->type() == QEvent::ContextMenu) {        QContextMenuEvent *c = static_cast<QContextMenuEvent *>(e);        QMenu *menu = m_lineEdit->createStandardContextMenu();        QList<QAction *> actions = menu->actions();        QListIterator<QAction *> itAction(actions);        while (itAction.hasNext()) {            QAction *action = itAction.next();            action->setShortcut(QKeySequence());            QString actionString = action->text();            int pos = actionString.lastIndexOf("\t");            if (pos > 0) {                actionString = actionString.mid(0, pos);            }            action->setText(actionString);        }        QAction *actionBefore = 0;        if (actions.count() > 0)            actionBefore = actions[0];        QAction *clearAction = new QAction(tr("Clear Shortcut"), menu);        menu->insertAction(actionBefore, clearAction);        menu->insertSeparator(actionBefore);        clearAction->setEnabled(!m_keySequence.isEmpty());        connect(clearAction, SIGNAL(triggered()), this, SLOT(slotClearShortcut()));        menu->exec(c->globalPos());        delete menu;        e->accept();        return true;    }    return QWidget::eventFilter(o, e);}void QtKeySequenceEdit::slotClearShortcut(){    setKeySequence(QKeySequence());}void QtKeySequenceEdit::handleKeyEvent(QKeyEvent *e){    int nextKey = e->key();    if (nextKey == Qt::Key_Control || nextKey == Qt::Key_Shift ||            nextKey == Qt::Key_Meta || nextKey == Qt::Key_Alt || nextKey == Qt::Key_Super_L)        return;    nextKey |= translateModifiers(e->modifiers());    int k0 = m_keySequence[0];    int k1 = m_keySequence[1];    int k2 = m_keySequence[2];    int k3 = m_keySequence[3];    switch (m_num) {        case 0: k0 = nextKey; k1 = 0; k2 = 0; k3 = 0; break;        case 1: k1 = nextKey; k2 = 0; k3 = 0; break;        case 2: k2 = nextKey; k3 = 0; break;        case 3: k3 = nextKey; break;        default: break;    }    ++m_num;    if (m_num > 3)        m_num = 0;    m_keySequence = QKeySequence(k0, k1, k2, k3);    m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));    e->accept();    emit keySequenceChanged(m_keySequence);}void QtKeySequenceEdit::setKeySequence(const QKeySequence &sequence){    if (sequence == m_keySequence)        return;    m_num = 0;    m_keySequence = sequence;    m_lineEdit->setText(m_keySequence.toString(QKeySequence::NativeText));}QKeySequence QtKeySequenceEdit::keySequence() const{    return m_keySequence;}int QtKeySequenceEdit::translateModifiers(Qt::KeyboardModifiers state) const{    int result = 0;    if (state & Qt::ShiftModifier)        result |= Qt::SHIFT;    if (state & Qt::ControlModifier)        result |= Qt::CTRL;    if (state & Qt::MetaModifier)        result |= Qt::META;    if (state & Qt::AltModifier)        result |= Qt::ALT;    return result;}void QtKeySequenceEdit::focusInEvent(QFocusEvent *e){    m_lineEdit->event(e);    m_lineEdit->selectAll();    QWidget::focusInEvent(e);}void QtKeySequenceEdit::focusOutEvent(QFocusEvent *e){    m_num = 0;    m_lineEdit->event(e);    QWidget::focusOutEvent(e);}void QtKeySequenceEdit::keyPressEvent(QKeyEvent *e){    handleKeyEvent(e);    e->accept();}void QtKeySequenceEdit::keyReleaseEvent(QKeyEvent *e){    m_lineEdit->event(e);}// -------------------------------------------------------------------------KeySequenceProperty::KeySequenceProperty(const QKeySequence &value, const QString &name)    : AbstractProperty<QKeySequence>(value, name){}void KeySequenceProperty::setValue(const QVariant &value){    m_value = qVariantValue<QKeySequence>(value);}QString KeySequenceProperty::toString() const{    return m_value.toString(QKeySequence::NativeText);}QWidget *KeySequenceProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QtKeySequenceEdit *keyEdit = new QtKeySequenceEdit(parent);    QObject::connect(keyEdit, SIGNAL(keySequenceChanged(QKeySequence)), target, receiver);    return keyEdit;}void KeySequenceProperty::updateEditorContents(QWidget *editor){    if (QtKeySequenceEdit *keyEdit = qobject_cast<QtKeySequenceEdit*>(editor)) {        keyEdit->setKeySequence(m_value);    }}void KeySequenceProperty::updateValue(QWidget *editor){    if (QtKeySequenceEdit *keyEdit = qobject_cast<QtKeySequenceEdit*>(editor)) {        const QKeySequence newValue = keyEdit->keySequence();        if (newValue != m_value) {            m_value = newValue;            setChanged(true);        }    }}// -------------------------------------------------------------------------CursorProperty::CursorProperty(const QCursor &value, const QString &name)    : AbstractProperty<QCursor>(value, name){}void CursorProperty::setValue(const QVariant &value){    m_value = qvariant_cast<QCursor>(value);}QString CursorProperty::toString() const{    return cursorName(m_value.shape());}QVariant CursorProperty::decoration() const{    return qVariantFromValue(cursorPixmap(m_value.shape()));}QWidget *CursorProperty::createEditor(QWidget *parent, const QObject *target, const char *receiver) const{    QComboBox *combo = new QComboBox(parent);    combo->view()->setTextElideMode(Qt::ElideLeft);    combo->setFrame(0);    addCursor(combo, Qt::ArrowCursor);    addCursor(combo, Qt::UpArrowCursor);    addCursor(combo, Qt::CrossCursor);    addCursor(combo, Qt::WaitCursor);    addCursor(combo, Qt::IBeamCursor);    addCursor(combo, Qt::SizeVerCursor);    addCursor(combo, Qt::SizeHorCursor);    addCursor(combo, Qt::SizeBDiagCursor);    addCursor(combo, Qt::SizeFDiagCursor);    addCursor(combo, Qt::SizeAllCursor);    addCursor(combo, Qt::BlankCursor);    addCursor(combo, Qt::SplitVCursor);    addCursor(combo, Qt::SplitHCursor);    addCursor(combo, Qt::PointingHandCursor);    addCursor(combo, Qt::ForbiddenCursor);    addCursor(combo, Qt::WhatsThisCursor);    addCursor(combo, Qt::BusyCursor);    addCursor(combo, Qt::OpenHandCursor);    addCursor(combo, Qt::ClosedHandCursor);    QObject::connect(combo, SIGNAL(activated(int)), target, receiver);    return combo;}void CursorProperty::updateEditorContents(QWidget *editor){    if (QComboBox *combo = qobject_cast<QComboBox*>(editor)) {        combo->setCurrentIndex(m_value.shape());    }}void CursorProperty::updateValue(QWidget *editor){    if (const QComboBox *combo = qobject_cast<const QComboBox*>(editor)) {        const QCursor newValue(static_cast<Qt::CursorShape>(combo->currentIndex()));        if (newValue.shape() != m_value.shape()) {            m_value = newValue;            setChanged(true);        }    }}QString CursorProperty::cursorName(int shape){    switch (shape) {    case Qt::ArrowCursor: return QString::fromUtf8("Arrow");    case Qt::UpArrowCursor: return QString::fromUtf8("Up-Arrow");    case Qt::CrossCursor: return QString::fromUtf8("Cross");    case Qt::WaitCursor: return QString::fromUtf8("Waiting");    case Qt::IBeamCursor: return QString::fromUtf8("IBeam");    case Qt::SizeVerCursor: return QString::fromUtf8("Size Vertical");    case Qt::SizeHorCursor: return QString::fromUtf8("Size Horizontal");    case Qt::SizeBDiagCursor: return QString::fromUtf8("Size Slash");    case Qt::SizeFDiagCursor: return QString::fromUtf8("Size Backslash");    case Qt::SizeAllCursor: return QString::fromUtf8("Size All");    case Qt::BlankCursor: return QString::fromUtf8("Blank");    case Qt::SplitVCursor: return QString::fromUtf8("Split Vertical");    case Qt::SplitHCursor: return QString::fromUtf8("Split Horizontal");    case Qt::PointingHandCursor: return QString::fromUtf8("Pointing Hand");    case Qt::ForbiddenCursor: return QString::fromUtf8("Forbidden");    case Qt::OpenHandCursor: return QString::fromUtf8("Open Hand");    case Qt::ClosedHandCursor: return QString::fromUtf8("Closed Hand");    case Qt::WhatsThisCursor: return QString::fromUtf8("Whats This");    case Qt::BusyCursor: return QString::fromUtf8("Busy");    default: return QString();    }}QPixmap CursorProperty::cursorPixmap(int shape){    switch (shape) {    case Qt::ArrowCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/arrow.png"));    case Qt::UpArrowCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/uparrow.png"));    case Qt::CrossCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/cross.png"));    case Qt::WaitCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/wait.png"));    case Qt::IBeamCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/ibeam.png"));    case Qt::SizeVerCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizev.png"));    case Qt::SizeHorCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeh.png"));    case Qt::SizeBDiagCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizef.png"));    case Qt::SizeFDiagCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeb.png"));    case Qt::SizeAllCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/sizeall.png"));    case Qt::BlankCursor:    {        QBitmap cur = QBitmap(25, 25);        cur.clear();        return cur;    }    case Qt::SplitVCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/vsplit.png"));    case Qt::SplitHCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/hsplit.png"));    case Qt::PointingHandCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/hand.png"));    case Qt::ForbiddenCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/no.png"));    case Qt::OpenHandCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/openhand.png"));    case Qt::ClosedHandCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/closedhand.png"));    case Qt::WhatsThisCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/whatsthis.png"));    case Qt::BusyCursor: return QPixmap(QString::fromUtf8(":/trolltech/formeditor/images/cursors/busy.png"));    default: return QPixmap();    }}void CursorProperty::addCursor(QComboBox *combo, int shape) const{    combo->addItem(cursorPixmap(shape), cursorName(shape), shape);}// -------------------------------------------------------------------------AlignmentProperty::AlignmentProperty(const QMap<QString, QVariant> &items, Qt::Alignment value, const QString &name)    : AbstractPropertyGroup(name){    QStringList horz_keys = QStringList()                            << matchStringInKeys(QLatin1String("AlignLeft"), items)                            << matchStringInKeys(QLatin1String("AlignRight"), items)                            << matchStringInKeys(QLatin1String("AlignHCenter"), items)                            << matchStringInKeys(QLatin1String("AlignJustify"), items);                                                 // << "Qt::AlignAbsolute"    QMap<QString, QVariant> horz_map;    foreach (QString h, horz_keys) {        horz_map.insert(h, items.value(h));    }    MapProperty *ph = new MapProperty(horz_map, uint(value & Qt::AlignHorizontal_Mask), QLatin1String("horizontal"));    ph->setFake(true);    ph->setParent(this);    m_properties << ph;    QStringList vert_keys = QStringList()                            << matchStringInKeys(QLatin1String("AlignTop"), items)                            << matchStringInKeys(QLatin1String("AlignBottom"), items)                            << matchStringInKeys(QLatin1String("AlignVCenter"), items);    QMap<QString, QVariant> vert_map;    foreach (QString h, vert_keys) {        vert_map.insert(h, items.value(h));    }    MapProperty *pv = new MapProperty(vert_map, int(value & Qt::AlignVertical_Mask), QLatin1String("vertical"));    pv->setFake(true);    pv->setParent(this);    m_properties << pv;}QVariant AlignmentProperty::value() const{    return uint(propertyAt(0)->value().toUInt() | propertyAt(1)->value().toUInt());}void AlignmentProperty::setValue(const QVariant &value){    QVariant v = value;    if (qVariantCanConvert<FlagType>(value))        v = qvariant_cast<FlagType>(value).value;    else if (qVariantCanConvert<EnumType>(value))        v = qvariant_cast<EnumType>(value).value;    propertyAt(0)->setValue(v.toUInt() & Qt::AlignHorizontal_Mask);    propertyAt(1)->setValue(v.toUInt() & Qt::AlignVertical_Mask);}// -------------------------------------------------------------------------DoubleProperty::DoubleProperty(double value, const QString &name)    : AbstractProperty<double>(value, name){}void DoubleProperty::setValue(const QVariant &value){    m_value = value.toDouble();}QString DoubleProperty::toString() const{    return QString::number(m_value);}

⌨️ 快捷键说明

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