Пример #1
0
QComboBox * TraceWire::createWidthComboBox(double m, QWidget * parent) 
{
	QComboBox * comboBox = new FocusOutComboBox(parent);  // new QComboBox(parent);
	comboBox->setEditable(true);
	QIntValidator * intValidator = new QIntValidator(comboBox);
	intValidator->setRange(MinTraceWidthMils, MaxTraceWidthMils);
	comboBox->setValidator(intValidator);

	int ix = 0;
	if (!Wire::widths.contains(m)) {
		Wire::widths.append(m);
		qSort(Wire::widths.begin(), Wire::widths.end());
	}
	foreach(long widthValue, Wire::widths) {
		QString widthName = Wire::widthTrans.value(widthValue, "");
		QVariant val((int) widthValue);
		comboBox->addItem(widthName.isEmpty() ? QString::number(widthValue) : widthName, val);
		if (qAbs(m - widthValue) < .01) {
			comboBox->setCurrentIndex(ix);
		}
		ix++;
	}
Пример #2
0
SimpleFontDialog::SimpleFontDialog(const QFont& initial, QWidget* parent)
    : QDialog(parent)
{
    QFontComboBox* fontComboBox = new QFontComboBox(this);
    fontComboBox->setCurrentFont(initial);
    font = initial;

    QVBoxLayout* familyLayout = new QVBoxLayout();
    familyLayout->addWidget(new QLabel(tr("Family")));
    familyLayout->addWidget(fontComboBox);

    QList<int> sizes = QFontDatabase::standardSizes();

    QComboBox* sizeComboBox = new QComboBox(this);
    sizeComboBox->setEditable(true);

    QIntValidator* sizeValidator = new QIntValidator(1, 512, this);
    sizeComboBox->setValidator(sizeValidator);

    int currentSizeIndex = 0;
    QFontInfo fontInfo(initial);
    bool currentFontInserted = false;

    for (int i = 0; i < sizes.size(); i++)
    {
        int size = sizes[i];

        // If the current font size is a non-standard size, then insert
        // it in the appropriate place in the sorted list of fonts.
        //
        if ((fontInfo.pointSize() < size) && !currentFontInserted)
        {
            currentSizeIndex = i;
            sizeComboBox->addItem(QString("%1").arg(fontInfo.pointSize()), fontInfo.pointSize());
            currentFontInserted = true;
        }
        // Else current font size is in the standard font sizes list.  Set
        // the index in the combo box to its position in the list.
        //
        else
        {
            if (fontInfo.pointSize() == size)
            {
                currentSizeIndex = i;
                currentFontInserted = true;
            }
        }

        sizeComboBox->addItem(QString("%1").arg(size), size);
    }

    if (!currentFontInserted)
    {
        sizeComboBox->addItem(QString("%1").arg(fontInfo.pointSize()), fontInfo.pointSize());
        currentSizeIndex = sizeComboBox->count() - 1;
    }

    sizeComboBox->setCurrentIndex(currentSizeIndex);

    QVBoxLayout* sizeLayout = new QVBoxLayout();
    sizeLayout->addWidget(new QLabel(tr("Size")));
    sizeLayout->addWidget(sizeComboBox);

    fontPreview = new QLineEdit(tr("AaBbCcXxYyZz"), this);
    fontPreview->setFont(initial);

    QVBoxLayout* previewLayout = new QVBoxLayout();
    previewLayout->addWidget(new QLabel(tr("Preview")));
    previewLayout->addWidget(fontPreview);

    QDialogButtonBox* buttonBox = new QDialogButtonBox(Qt::Horizontal, this);
    buttonBox->addButton(QDialogButtonBox::Ok);
    buttonBox->addButton(QDialogButtonBox::Cancel);

    QGridLayout* layout = new QGridLayout();
    layout->addItem(familyLayout, 0, 0);
    layout->addItem(sizeLayout, 0, 1);
    layout->addItem(previewLayout, 1, 0, 1, 2, Qt::AlignCenter);
    layout->addWidget(buttonBox, 2, 0, 1, 2);

    setLayout(layout);

    connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
    connect(fontComboBox, SIGNAL(currentFontChanged(QFont)), this, SLOT(onFontFamilyChanged(const QFont&)));
    connect(sizeComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(onFontSizeChanged(const QString&)));
    connect(sizeComboBox, SIGNAL(editTextChanged(QString)), this, SLOT(onFontSizeChanged(const QString&)));
}
Пример #3
0
QWidget *QgsAttributeEditor::createAttributeEditor( QWidget *parent, QWidget *editor, QgsVectorLayer *vl, int idx, const QVariant &value, QMap<int, QWidget*> &proxyWidgets )
{
    if ( !vl )
        return 0;

    QWidget *myWidget = 0;
    QgsVectorLayer::EditType editType = vl->editType( idx );
    const QgsField &field = vl->pendingFields()[idx];
    QVariant::Type myFieldType = field.type();

    bool synchronized = false;

    switch ( editType )
    {
    case QgsVectorLayer::UniqueValues:
    {
        QList<QVariant> values;
        vl->dataProvider()->uniqueValues( idx, values );

        QComboBox *cb = comboBox( editor, parent );
        if ( cb )
        {
            cb->setEditable( false );

            for ( QList<QVariant>::iterator it = values.begin(); it != values.end(); it++ )
                cb->addItem( it->toString(), it->toString() );

            myWidget = cb;
        }

    }
    break;

    case QgsVectorLayer::Enumeration:
    {
        QStringList enumValues;
        vl->dataProvider()->enumValues( idx, enumValues );

        QComboBox *cb = comboBox( editor, parent );
        if ( cb )
        {
            QStringList::const_iterator s_it = enumValues.constBegin();
            for ( ; s_it != enumValues.constEnd(); ++s_it )
            {
                cb->addItem( *s_it, *s_it );
            }

            myWidget = cb;
        }
    }
    break;

    case QgsVectorLayer::ValueMap:
    {
        const QMap<QString, QVariant> &map = vl->valueMap( idx );

        QComboBox *cb = comboBox( editor, parent );
        if ( cb )
        {
            for ( QMap<QString, QVariant>::const_iterator it = map.begin(); it != map.end(); it++ )
            {
                cb->addItem( it.key(), it.value() );
            }

            myWidget = cb;
        }
    }
    break;

    case QgsVectorLayer::ValueRelation:
    {
        const QgsVectorLayer::ValueRelationData &data = vl->valueRelation( idx );

        QgsVectorLayer *layer = qobject_cast<QgsVectorLayer*>( QgsMapLayerRegistry::instance()->mapLayer( data.mLayer ) );
        QMap< QString, QString > map;

        int fi = -1;
        if ( layer )
        {
            int ki = layer->fieldNameIndex( data.mOrderByValue ? data.mValue : data.mKey );
            int vi = layer->fieldNameIndex( data.mOrderByValue ? data.mKey : data.mValue );

            if ( !data.mFilterAttributeColumn.isNull() )
                fi = layer->fieldNameIndex( data.mFilterAttributeColumn );

            if ( ki >= 0 && vi >= 0 )
            {
                QgsAttributeList attributes;
                attributes << ki;
                attributes << vi;
                if ( fi >= 0 )
                    attributes << fi;

                QgsFeatureIterator fit = layer->getFeatures( QgsFeatureRequest().setFlags( QgsFeatureRequest::NoGeometry ).setSubsetOfAttributes( attributes ) );
                QgsFeature f;
                while ( fit.nextFeature( f ) )
                {
                    if ( fi >= 0 && f.attribute( fi ).toString() != data.mFilterAttributeValue )
                        continue;

                    map.insert( f.attribute( ki ).toString(), f.attribute( vi ).toString() );
                }
            }
        }

        if ( !data.mAllowMulti )
        {
            QComboBox *cb = comboBox( editor, parent );
            if ( cb )
            {
                if ( data.mAllowNull )
                {
                    QSettings settings;
                    cb->addItem( tr( "(no selection)" ), settings.value( "qgis/nullValue", "NULL" ).toString() );
                }

                for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
                {
                    if ( data.mOrderByValue )
                        cb->addItem( it.key(), it.value() );
                    else
                        cb->addItem( it.value(), it.key() );
                }

                myWidget = cb;
            }
        }
        else
        {
            QListWidget *lw = listWidget( editor, parent );
            if ( lw )
            {
                QStringList checkList = value.toString().remove( QChar( '{' ) ).remove( QChar( '}' ) ).split( "," );

                for ( QMap< QString, QString >::const_iterator it = map.begin(); it != map.end(); it++ )
                {
                    QListWidgetItem *item;
                    if ( data.mOrderByValue )
                    {
                        item = new QListWidgetItem( it.key() );
                        item->setData( Qt::UserRole, it.value() );
                        item->setCheckState( checkList.contains( it.value() ) ? Qt::Checked : Qt::Unchecked );
                    }
                    else
                    {
                        item = new QListWidgetItem( it.value() );
                        item->setData( Qt::UserRole, it.key() );
                        item->setCheckState( checkList.contains( it.key() ) ? Qt::Checked : Qt::Unchecked );
                    }
                    lw->addItem( item );
                }

                myWidget = lw;
            }
        }
    }
    break;

    case QgsVectorLayer::Classification:
    {
        QMap<QString, QString> classes;

        const QgsUniqueValueRenderer *uvr = dynamic_cast<const QgsUniqueValueRenderer *>( vl->renderer() );
        if ( uvr )
        {
            const QList<QgsSymbol *> symbols = uvr->symbols();

            for ( int i = 0; i < symbols.size(); i++ )
            {
                QString label = symbols[i]->label();
                QString name = symbols[i]->lowerValue();

                if ( label == "" )
                    label = name;

                classes.insert( name, label );
            }
        }

        const QgsCategorizedSymbolRendererV2 *csr = dynamic_cast<const QgsCategorizedSymbolRendererV2 *>( vl->rendererV2() );
        if ( csr )
        {
            const QgsCategoryList &categories = (( QgsCategorizedSymbolRendererV2 * )csr )->categories(); // FIXME: QgsCategorizedSymbolRendererV2::categories() should be const
            for ( int i = 0; i < categories.size(); i++ )
            {
                QString label = categories[i].label();
                QString value = categories[i].value().toString();
                if ( label.isEmpty() )
                    label = value;
                classes.insert( value, label );
            }
        }

        QComboBox *cb = comboBox( editor, parent );
        if ( cb )
        {
            for ( QMap<QString, QString>::const_iterator it = classes.begin(); it != classes.end(); it++ )
            {
                cb->addItem( it.value(), it.key() );
            }

            myWidget = cb;
        }
    }
    break;

    case QgsVectorLayer::DialRange:
    case QgsVectorLayer::SliderRange:
    case QgsVectorLayer::EditRange:
    {
        if ( myFieldType == QVariant::Int )
        {
            int min = vl->range( idx ).mMin.toInt();
            int max = vl->range( idx ).mMax.toInt();
            int step = vl->range( idx ).mStep.toInt();

            if ( editType == QgsVectorLayer::EditRange )
            {
                QSpinBox *sb = 0;

                if ( editor )
                    sb = qobject_cast<QSpinBox *>( editor );
                else
                    sb = new QSpinBox( parent );

                if ( sb )
                {
                    sb->setRange( min, max );
                    sb->setSingleStep( step );

                    myWidget = sb;
                }
            }
            else
            {
                QAbstractSlider *sl = 0;

                if ( editor )
                {
                    sl = qobject_cast<QAbstractSlider*>( editor );
                }
                else if ( editType == QgsVectorLayer::DialRange )
                {
                    sl = new QDial( parent );
                }
                else
                {
                    sl = new QSlider( Qt::Horizontal, parent );
                }

                if ( sl )
                {
                    sl->setRange( min, max );
                    sl->setSingleStep( step );

                    myWidget = sl;
                }
            }
            break;
        }
        else if ( myFieldType == QVariant::Double )
        {
            QDoubleSpinBox *dsb = 0;
            if ( editor )
                dsb = qobject_cast<QDoubleSpinBox*>( editor );
            else
                dsb = new QDoubleSpinBox( parent );

            if ( dsb )
            {
                double min = vl->range( idx ).mMin.toDouble();
                double max = vl->range( idx ).mMax.toDouble();
                double step = vl->range( idx ).mStep.toDouble();

                dsb->setRange( min, max );
                dsb->setSingleStep( step );

                myWidget = dsb;
            }
            break;
        }
    }

    case QgsVectorLayer::CheckBox:
    {
        QCheckBox *cb = 0;
        QGroupBox *gb = 0;
        if ( editor )
        {
            gb = qobject_cast<QGroupBox *>( editor );
            cb = qobject_cast<QCheckBox*>( editor );
        }
        else
            cb = new QCheckBox( parent );

        if ( cb )
        {
            myWidget = cb;
            break;
        }
        else if ( gb )
        {
            myWidget = gb;
            break;
        }
    }

    // fall-through

    case QgsVectorLayer::LineEdit:
    case QgsVectorLayer::TextEdit:
    case QgsVectorLayer::UuidGenerator:
    case QgsVectorLayer::UniqueValuesEditable:
    case QgsVectorLayer::Immutable:
    {
        QLineEdit *le = 0;
        QTextEdit *te = 0;
        QPlainTextEdit *pte = 0;
        QComboBox * cb = 0;

        if ( editor )
        {
            le = qobject_cast<QLineEdit *>( editor );
            te = qobject_cast<QTextEdit *>( editor );
            pte = qobject_cast<QPlainTextEdit *>( editor );
            cb = qobject_cast<QComboBox *>( editor );
        }
        else if ( editType == QgsVectorLayer::TextEdit )
        {
            pte = new QPlainTextEdit( parent );
        }
        else
        {
            le = new QLineEdit( parent );
        }

        if ( le )
        {
            if ( editType == QgsVectorLayer::UniqueValuesEditable )
            {
                QList<QVariant> values;
                vl->dataProvider()->uniqueValues( idx, values );

                QStringList svalues;
                for ( QList<QVariant>::const_iterator it = values.begin(); it != values.end(); it++ )
                    svalues << it->toString();

                QCompleter *c = new QCompleter( svalues );
                c->setCompletionMode( QCompleter::PopupCompletion );
                le->setCompleter( c );
            }

            if ( editType == QgsVectorLayer::UuidGenerator )
            {
                le->setReadOnly( true );
            }

            le->setValidator( new QgsFieldValidator( le, field ) );

            myWidget = le;
        }

        if ( te )
        {
            te->setAcceptRichText( true );
            myWidget = te;
        }

        if ( pte )
        {
            myWidget = pte;
        }

        if ( cb )
        {
            if ( cb->isEditable() )
                cb->setValidator( new QgsFieldValidator( cb, field ) );
            myWidget = cb;
        }

        if ( myWidget )
        {
            myWidget->setDisabled( editType == QgsVectorLayer::Immutable );

            QgsStringRelay* relay = NULL;

            QMap<int, QWidget*>::const_iterator it = proxyWidgets.find( idx );
            if ( it != proxyWidgets.end() )
            {
                QObject* obj = qvariant_cast<QObject*>(( *it )->property( "QgisAttrEditProxy" ) );
                relay = qobject_cast<QgsStringRelay*>( obj );
            }
            else
            {
                relay = new QgsStringRelay( myWidget );
            }

            const char* rSlot = SLOT( changeText( QString ) );
            const char* rSig = SIGNAL( textChanged( QString ) );
            const char* wSlot = SLOT( setText( QString ) );
            const char* wSig = SIGNAL( textChanged( QString ) );
            if ( te || pte )
            {
                rSlot = SLOT( changeText() );
                wSig = SIGNAL( textChanged() );
            }
            if ( pte )
            {
                wSlot = SLOT( setPlainText( QString ) );
            }
            if ( cb && cb->isEditable() )
            {
                wSlot = SLOT( setEditText( QString ) );
                wSig = SIGNAL( editTextChanged( QString ) );
            }

            synchronized =  connect( relay, rSig, myWidget, wSlot );
            synchronized &= connect( myWidget, wSig, relay, rSlot );

            // store list of proxies in relay
            relay->appendProxy( myWidget );

            if ( !cb || cb->isEditable() )
            {
                myWidget->setProperty( "QgisAttrEditSlot", QVariant( QByteArray( wSlot ) ) );
                myWidget->setProperty( "QgisAttrEditProxy", QVariant( QMetaType::QObjectStar, &relay ) );
            }
        }
    }
    break;

    case QgsVectorLayer::Hidden:
        myWidget = 0;
        break;

    case QgsVectorLayer::FileName:
    case QgsVectorLayer::Calendar:
    {
        QCalendarWidget *cw = qobject_cast<QCalendarWidget *>( editor );
        if ( cw )
        {
            myWidget = cw;
            break;
        }


        QPushButton *pb = 0;
        QLineEdit *le = qobject_cast<QLineEdit *>( editor );
        if ( le )
        {
            if ( le )
                myWidget = le;

            if ( editor->parent() )
            {
                pb = editor->parent()->findChild<QPushButton *>();
            }
        }
        else
        {
            le = new QLineEdit();

            pb = new QPushButton( tr( "..." ) );

            QHBoxLayout *hbl = new QHBoxLayout();
            hbl->addWidget( le );
            hbl->addWidget( pb );

            myWidget = new QWidget( parent );
            myWidget->setBackgroundRole( QPalette::Window );
            myWidget->setAutoFillBackground( true );
            myWidget->setLayout( hbl );
        }

        if ( pb )
        {
            if ( editType == QgsVectorLayer::FileName )
                connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectFileName() ) );
            if ( editType == QgsVectorLayer::Calendar )
                connect( pb, SIGNAL( clicked() ), new QgsAttributeEditor( pb ), SLOT( selectDate() ) );
        }
    }
    break;
    }

    QMap<int, QWidget*>::const_iterator it = proxyWidgets.find( idx );
    if ( it != proxyWidgets.end() )
    {
        if ( !synchronized )
        {
            myWidget->setEnabled( false );
        }
    }
    else
    {
        proxyWidgets.insert( idx, myWidget );
    }

    setValue( myWidget, vl, idx, value );

    return myWidget;
}