void ContactDialog::addTriplet(int& count, QGridLayout* l, const QString& nameTemplate, const QString& itemValue)
{
    if (count>=MIN_VISIBLE_TRIPLETS) {
        // Value
        QLineEdit* le = new QLineEdit(this);
        le->setObjectName(QString("le%1%2").arg(nameTemplate).arg(count+1));
        l->addWidget(le, count, 0);
        // Item type combo box
        QComboBox* cbT = new QComboBox(this);
        cbT->setObjectName(QString("cb%1Type%2").arg(nameTemplate).arg(count+1));
        cbT->setMaxVisibleItems(32); // "mixed" (last item) must be strongly visible
        connect(cbT, SIGNAL(currentIndexChanged(const QString&)), this, SLOT(itemTypeChanged(const QString&)));
        l->addWidget(cbT, count, 1);
        // Delete button
        QToolButton* btnD = addDelButton(count, nameTemplate, SLOT(slotDelTriplet()));
        l->addWidget(btnD, count, 2);
    }
Example #2
0
void ItemHandlerCombobox::Handle (const QDomElement& item, QWidget *pwidget)
{
    QGridLayout *lay = qobject_cast<QGridLayout*> (pwidget->layout ());

    QHBoxLayout *hboxLay = new QHBoxLayout;
    QComboBox *box = new QComboBox (XSD_->GetWidget ());

    hboxLay->addWidget (box);

    XSD_->SetTooltip (box, item);
    box->setObjectName (item.attribute ("property"));
    box->setSizeAdjustPolicy (QComboBox::AdjustToContents);
    if (item.hasAttribute ("maxVisibleItems"))
        box->setMaxVisibleItems (item.attribute ("maxVisibleItems").toInt ());

    bool mayHaveDataSource = item.hasAttribute ("mayHaveDataSource") &&
                             item.attribute ("mayHaveDataSource").toLower () == "true";
    if (mayHaveDataSource)
    {
        const QString& prop = item.attribute ("property");
        Factory_->RegisterDatasourceSetter (prop,
                                            [this] (const QString& str, QAbstractItemModel *m, Util::XmlSettingsDialog *xsd)
        {
            SetDataSource (str, m, xsd);
        });
        Propname2Combobox_ [prop] = box;
        Propname2Item_ [prop] = item;
    }

    hboxLay->addStretch ();

    if (item.hasAttribute ("moreThisStuff"))
    {
        QPushButton *moreButt = new QPushButton (tr ("More stuff..."));
        hboxLay->addWidget (moreButt);

        moreButt->setObjectName (item.attribute ("moreThisStuff"));
        connect (moreButt,
                 SIGNAL (released ()),
                 XSD_,
                 SLOT (handleMoreThisStuffRequested ()));
    }

    QDomElement option = item.firstChildElement ("option");
    while (!option.isNull ())
    {
        const auto& images = XSD_->GetImages (option);
        const auto& name = option.attribute ("name");

        auto label = XSD_->GetLabel (option);
        if (label.isEmpty ())
            label = name;

        if (!images.isEmpty ())
            box->addItem (QPixmap::fromImage (images.at (0)), label, name);
        else
            box->addItem (label, name);

        auto setColor = [&option, box] (const QString& attr, Qt::ItemDataRole role) -> void
        {
            if (option.hasAttribute (attr))
            {
                const QColor color (option.attribute (attr));
                box->setItemData (box->count () - 1, color, role);
            }
        };
        setColor ("color", Qt::ForegroundRole);
        setColor ("bgcolor", Qt::BackgroundRole);

        option = option.nextSiblingElement ("option");
    }

    connect (box,
             SIGNAL (currentIndexChanged (int)),
             this,
             SLOT (updatePreferences ()));

    QDomElement scriptContainer = item.firstChildElement ("scripts");
    if (!scriptContainer.isNull ())
    {
        Scripter scripter (scriptContainer);

        for (const auto& elm : scripter.GetOptions ())
            box->addItem (scripter.HumanReadableOption (elm), elm);
    }

    int pos = box->findData (XSD_->GetValue (item));
    if (pos != -1)
        box->setCurrentIndex (pos);
    else if (!mayHaveDataSource)
        qWarning () << Q_FUNC_INFO
                    << box
                    << XSD_->GetValue (item)
                    << "not found (and this item may not have a datasource)";

    QLabel *label = new QLabel (XSD_->GetLabel (item));
    label->setWordWrap (false);

    box->setProperty ("ItemHandler", QVariant::fromValue<QObject*> (this));
    box->setProperty ("SearchTerms", label->text ());

    int row = lay->rowCount ();
    lay->addWidget (label, row, 0, Qt::AlignRight);
    lay->addLayout (hboxLay, row, 1);
}
Example #3
0
void AsciiOpenDlg::updateTable(const QString &separator)
{
    tableWidget->setEnabled(false);
    extractSFNamesFrom1stLineCheckBox->setEnabled(false);
    m_headerLine.clear();

    if (m_filename.isEmpty())
    {
        tableWidget->clear();
        return;
    }

    if (separator.length()<1)
    {
        asciiCodeLabel->setText("Enter a valid character!");
        buttonBox->setEnabled(false);
        tableWidget->clear();
        m_invalidColumns = true;
        return;
    }

    //we open the file in ASCII mode
    FILE* pFile = fopen(qPrintable(m_filename),"rt");
    if (!pFile)
    {
        tableWidget->clear();
        m_invalidColumns = true;
        return;
    }

    //buffer
    char line[MAX_ASCII_FILE_LINE_LENGTH];      //last read line

    //we skip first lines (if needed)
    unsigned i;
    for (i=0; i<m_skippedLines; ++i)
    {
        if (fgets(line, MAX_ASCII_FILE_LINE_LENGTH, pFile))
        {
            //we keep track of the first line
            if (i==0)
                m_headerLine = QString(line);
        }
        else
        {
            fclose(pFile);
            tableWidget->clear();
            m_invalidColumns = true;
            return;
        }
    }

    //new separator
    m_separator = separator[0];
    asciiCodeLabel->setText(QString("(ASCII code: %1)").arg(m_separator.unicode()));
    //if the old setup has less than 3 columns, we forget it
    if (m_columnsCount<3)
    {
        tableWidget->clear();
        m_columnsCount=0;
    }
    tableWidget->setRowCount(DISPLAYED_LINES+1);    //+1 for first line shifting

    unsigned lineCount = 0;			//number of lines read
    unsigned totalChars = 0;        //total read characters (for stats)
    unsigned columnsCount = 0;		//max columns count per line

    std::vector<bool> valueIsNumber;	//identifies columns with numbers only [mandatory]
    std::vector<bool> valueIsBelowOne;	//identifies columns with values between -1 and 1 only
    std::vector<bool> valueIsInteger;	//identifies columns with integer values only
    std::vector<bool> valueIsBelow255;	//identifies columns with integer values between 0 and 255 only

    while ((lineCount<LINES_READ_FOR_STATS) && fgets(line, MAX_ASCII_FILE_LINE_LENGTH, pFile))
    {
        //we convert char buffer to a QString object
        QString str(line);

        //we recognize "//" as comment
        if (line[0]!='/' || line[1]!='/')
        {
            QStringList parts = str.trimmed().split(m_separator,QString::SkipEmptyParts);
            unsigned partsCount = (unsigned)parts.size();
            if (partsCount>MAX_COLUMNS)
                partsCount=MAX_COLUMNS;

            if (lineCount<DISPLAYED_LINES)
            {
                //do we need to add one or several new columns?
                if (partsCount>columnsCount)
                {
                    //we also extend vectors
                    for (i=columnsCount; i<partsCount; ++i)
                    {
                        valueIsNumber.push_back(true);
                        valueIsBelowOne.push_back(true);
                        valueIsBelow255.push_back(true);
                        valueIsInteger.push_back(true);
                    }

                    tableWidget->setColumnCount(partsCount);
                    columnsCount=partsCount;
                }

                //we fill row with extracted parts
                for (i=0; i<partsCount; ++i)
                {
                    QTableWidgetItem *newItem = new QTableWidgetItem(parts[i]);

                    //test values
                    bool isANumber = false;
                    double value = parts[i].toDouble(&isANumber);
                    if (!isANumber)
                    {
                        valueIsNumber[i]	= false;
                        valueIsBelowOne[i]	= false;
                        valueIsInteger[i]	= false;
                        valueIsBelow255[i]	= false;
                        newItem->setBackground(QBrush(QColor(255,160,160)));
                    }
                    else
                    {
                        double intPart, fracPart;
                        fracPart = modf(value,&intPart);

                        valueIsBelowOne[i]	= valueIsBelowOne[i] && (fabs(value)<=1.0);
                        valueIsInteger[i]	= valueIsInteger[i] && (fracPart == 0.0);
                        valueIsBelow255[i]	= valueIsBelow255[i] && (valueIsInteger[i] && (intPart >= 0.0 && value<=255.0));
                    }

                    tableWidget->setItem(lineCount+1, i, newItem); //+1 for first line shifting
                }
            }

            totalChars += (str.size()+1); //+1 for return char at eol

            ++lineCount;
        }
    }

    fclose(pFile);
    pFile=0;

    if (lineCount==0 || columnsCount==0)
    {
        m_averageLineSize = -1.0;
        tableWidget->clear();
        m_invalidColumns = true;
        return;
    }

    //average line size
    m_averageLineSize = double(totalChars)/double(lineCount);

    //we add a type selector for each column
    QStringList propsText;
    for (i=0; i<ASCII_OPEN_DLG_TYPES_NUMBER; i++)
        propsText << QString(ASCII_OPEN_DLG_TYPES_NAMES[i]);

    //remove unnecessary columns
    while (columnsCount<m_columnsCount)
        tableWidget->removeColumn(--m_columnsCount);
    for (i=lineCount+1; i<=DISPLAYED_LINES; ++i)
        tableWidget->removeRow(i);

    int columnWidth = (tableWidget->width()*9) / (columnsCount*10);
    columnWidth = ccMax(columnWidth,80);

    //Icons
    const QIcon xIcon(QString::fromUtf8(":/CC/Types/images/types/x_coordinate.png"));
    const QIcon yIcon(QString::fromUtf8(":/CC/Types/images/types/y_coordinate.png"));
    const QIcon zIcon(QString::fromUtf8(":/CC/Types/images/types/z_coordinate.png"));
    const QIcon NormIcon(QString::fromUtf8(":/CC/Types/images/types/normal.png"));
    const QIcon RGBIcon(QString::fromUtf8(":/CC/Types/images/types/rgb_color.png"));
    const QIcon GreyIcon(QString::fromUtf8(":/CC/Types/images/types/gray_color.png"));
    const QIcon ScalarIcon(QString::fromUtf8(":/CC/Types/images/types/scalar_field.png"));
    const QIcon PositiveScalarIcon(QString::fromUtf8(":/CC/Types/images/types/positive_scalar_field.png"));

    unsigned assignedXYZ = 0;
    unsigned assignedNorm = 0;
    unsigned assignedRGB = 0;
    for (i=0; i<columnsCount; i++)
    {
        QComboBox* columnHeader = static_cast<QComboBox*>(tableWidget->cellWidget(0,i));
        QComboBox* _columnHeader = columnHeader;
        if (!columnHeader)
        {
            columnHeader = new QComboBox();
            columnHeader->addItems(propsText);
            columnHeader->setMaxVisibleItems(ASCII_OPEN_DLG_TYPES_NUMBER);
            columnHeader->setCurrentIndex(0);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_X,xIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_Y,yIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_Z,zIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_NX,NormIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_NY,NormIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_NZ,NormIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_R,RGBIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_G,RGBIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_B,RGBIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_Grey,GreyIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_Scalar,ScalarIcon);
            columnHeader->setItemIcon(ASCII_OPEN_DLG_Positive_Scalar,PositiveScalarIcon);

            connect(columnHeader, SIGNAL(currentIndexChanged(int)), this, SLOT(columnsTypeHasChanged(int)));
        }

        if (valueIsNumber[i])
        {
            //first time? let's try to assign each column a type
            if ((m_invalidColumns || m_columnsCount==0) && columnsCount>1)
            {
                columnHeader->blockSignals(true);
                //by default, we assume that the first columns are always X,Y and Z
                if (assignedXYZ<3)
                {
                    //in rare cases, the first column is an index
                    if (assignedXYZ==0 && valueIsInteger[i] && (i+1<columnsCount) && !valueIsInteger[i+1])
                    {
                        //we skip it
                    }
                    else
                    {
                        ++assignedXYZ;
                        columnHeader->setCurrentIndex(assignedXYZ);
                    }
                }
                else
                {
                    //looks like RGB?
                    if (valueIsBelow255[i] && assignedRGB<3 && (i+2-assignedRGB < columnsCount)
                            && (assignedRGB > 0 || (valueIsBelow255[i+1] && valueIsBelow255[i+2]))) //make sure that next values are also ok!
                    {
                        columnHeader->setCurrentIndex(ASCII_OPEN_DLG_R+assignedRGB);
                        ++assignedRGB;
                    }
                    else if (valueIsBelowOne[i] && assignedNorm<3 && (i+2-assignedNorm < columnsCount)
                             && (assignedNorm > 0 || (valueIsBelowOne[i+1] && valueIsBelowOne[i+2]))) //make sure that next values are also ok!
                    {
                        columnHeader->setCurrentIndex(ASCII_OPEN_DLG_NX+assignedNorm);
                        ++assignedNorm;
                    }
                    else
                    {
                        //maybe it's a scalar?
                        columnHeader->setCurrentIndex(ASCII_OPEN_DLG_Scalar);
                    }
                }
                columnHeader->blockSignals(false);
            }
        }

        if (!_columnHeader)
            tableWidget->setCellWidget(0,i,columnHeader);
        tableWidget->setColumnWidth(i,columnWidth);
    }
Example #4
0
QWidget *AnemDelegate::createEditor(QWidget* parent,
                                     const QStyleOptionViewItem& option,
                                     const QModelIndex& index) const
{
    Q_UNUSED(option)

    QLabel *label;
    QComboBox *combo;
    QDoubleSpinBox *dspin;
    QLineEdit *ledit;
    QString currentManufacturer = index.model()->data(index.model()->index(AnemModel::MANUFACTURER, index.column())).toString();
    QString currentModel = index.model()->data(index.model()->index(AnemModel::MODEL, index.column())).toString();

    // can only edit name on blank column
    if (index.column() >= index.model()->columnCount()) return 0;

    // different kind of editor for each row
    switch (index.row())
    {
      case AnemModel::MANUFACTURER:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          combo->addItems(AnemDesc::manufacturerStringList());
          combo->view()->setTextElideMode(Qt::ElideNone);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case AnemModel::MODEL:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          if (currentManufacturer.isEmpty())
          {
              combo->addItems(AnemDesc::allModelStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_0())
          {
              combo->addItems(AnemDesc::campbellModelStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_1())
          {
              combo->addItems(AnemDesc::gillModelStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_2())
          {
              combo->addItems(AnemDesc::metekModelStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_3())
          {
              combo->addItems(AnemDesc::youngModelStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_4())
          {
              combo->addItems(AnemDesc::otherModelStringList());
          }
          combo->view()->setTextElideMode(Qt::ElideNone);
          combo->setMaxVisibleItems(15);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case AnemModel::ID:
          ledit = new QLineEdit(parent);
          connect(ledit, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return ledit;
      case AnemModel::HEIGHT:
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(2);
          dspin->setRange(0.1, 500.0);
          dspin->setSingleStep(1.0);
          dspin->setAccelerated(true);
          dspin->setSuffix(QStringLiteral(" [m]"));
          connect(dspin, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return dspin;
      case AnemModel::WINDFORMAT:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          if (currentManufacturer.isEmpty()
              || currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_1())
          {
              combo->addItems(AnemDesc::allWindFormatStringList());
          }
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_0())
          {
              combo->addItems(AnemDesc::simplestWindFormatStringList());
          }
          else
          {
              combo->addItems(AnemDesc::commonWindFormatStringList());
          }
          combo->view()->setTextElideMode(Qt::ElideNone);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case AnemModel::NORTHALIGNMENT:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          if (currentManufacturer.isEmpty())
          {
              combo->addItems(AnemDesc::allNorthAlignmentStringList());
          }
          // gill
          else if (currentManufacturer == AnemDesc::getANEM_MANUFACTURER_STRING_1())
          {
              combo->addItems(AnemDesc::gillNorthAlignmentStringList());
          }
          else
          {
              combo->addItems(AnemDesc::naNorthAlignmentStringList());
          }
          combo->view()->setTextElideMode(Qt::ElideNone);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case AnemModel::NORTHOFFSET:
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(1);
          dspin->setRange(0.0, 360.0);
          dspin->setSingleStep(1.0);
          dspin->setAccelerated(true);
          dspin->setSuffix(tr("  [%1]", "Degrees").arg(Defs::DEGREE));
          connect(dspin, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return dspin;
      case AnemModel::NSEPARATION:
      case AnemModel::ESEPARATION:
      case AnemModel::VSEPARATION:
          if (index.column() == 0)
          {
              label = new QLabel(parent);
              return label;
          }
          else
          {
              dspin = new QDoubleSpinBox(parent);
              dspin->setDecimals(2);
              dspin->setRange(-1000.0, 1000.0);
              dspin->setSingleStep(1.0);
              dspin->setAccelerated(true);
              dspin->setSuffix(QStringLiteral(" [cm]"));
              connect(dspin, SIGNAL(editingFinished()),
                      this, SLOT(commitAndCloseEditor()));
              return dspin;
          }
      case AnemModel::VPATHLENGTH:
      case AnemModel::HPATHLENGTH:
        if (currentModel != AnemDesc::getANEM_MODEL_STRING_12())
        {
            label = new QLabel(parent);
            return label;
        }
        else
        {
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(4);
          dspin->setRange(0.0001, 1000.0);
          dspin->setSingleStep(1.0);
          dspin->setAccelerated(true);
          dspin->setSuffix(QStringLiteral(" [cm]"));
          connect(dspin, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return dspin;
        }
      case AnemModel::TAU:
        if (currentModel != AnemDesc::getANEM_MODEL_STRING_12())
        {
            label = new QLabel(parent);
            return label;
        }
        else
        {
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(4);
          dspin->setRange(0.001, 1000.0);
          dspin->setSingleStep(1.0);
          dspin->setAccelerated(true);
          dspin->setSuffix(QStringLiteral(" [s]"));
          connect(dspin, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return dspin;
        }
        default:
          return 0;
    }
}
Example #5
0
int drv_combobox(int drvid, void *a0, void* a1, void* a2, void* a3, void* a4, void* a5, void* a6, void* a7, void* a8, void* a9)
{
    handle_head* head = (handle_head*)a0;
    QComboBox *self = (QComboBox*)head->native;
    switch (drvid) {
    case COMBOBOX_INIT: {
        drvNewObj(a0,new QComboBox);
        break;
    }
    case COMBOBOX_COUNT: {
        drvSetInt(a1,self->count());
        break;
    }
    case COMBOBOX_SETCURRENTINDEX: {
        self->setCurrentIndex(drvGetInt(a1));
        break;
    }
    case COMBOBOX_CURRENTINDEX: {
        drvSetInt(a1,self->currentIndex());
        break;
    }
    case COMBOBOX_CURRENTTEXT: {
        drvSetString(a1,self->currentText());
        break;
    }
    case COMBOBOX_SETEDITABLE: {
        self->setEditable(drvGetBool(a1));
        break;
    }
    case COMBOBOX_ISEDITABLE: {
        drvSetBool(a1,self->isEditable());
        break;
    }
    case COMBOBOX_SETMAXCOUNT: {
        self->setMaxCount(drvGetInt(a1));
        break;
    }
    case COMBOBOX_MAXCOUNT: {
        drvSetInt(a1,self->maxCount());
        break;
    }
    case COMBOBOX_SETMAXVISIBLEITEMS: {
        self->setMaxVisibleItems(drvGetInt(a1));
        break;
    }
    case COMBOBOX_MAXVISIBLEITEMS: {
        drvSetInt(a1,self->maxVisibleItems());
        break;
    }
    case COMBOBOX_SETMINIMUMCONTENTSLENGTH: {
        self->setMinimumContentsLength(drvGetInt(a1));
        break;
    }
    case COMBOBOX_MINIMUNCONTENTSLENGHT: {
        drvSetInt(a1,self->minimumContentsLength());
        break;
    }
    case COMBOBOX_ADDITEM: {
        self->addItem(drvGetString(a1));
        break;
    }
    case COMBOBOX_INSERTITEM: {
        self->insertItem(drvGetInt(a1),drvGetString(a2));
        break;
    }
    case COMBOBOX_REMOVEITEM: {
        self->removeItem(drvGetInt(a1));
        break;
    }
    case COMBOBOX_ITEMTEXT: {
        drvSetString(a2,self->itemText(drvGetInt(a1)));
        break;
    }
    case COMBOBOX_ONCURRENTINDEXCHANGED: {
        QObject::connect(self,SIGNAL(currentIndexChanged(int)),drvNewSignal(self,a1,a2),SLOT(call(int)));
        break;
    }
    default:
        return 0;
    }
    return 1;
}
Example #6
0
void AsciiOpenDlg::updateTable()
{
	m_ui->tableWidget->setEnabled(false);
	m_ui->extractSFNamesFrom1stLineCheckBox->setEnabled(false);

	bool hadValidHeader = !m_headerLine.isEmpty();
	m_headerLine.clear();

	if (m_filename.isEmpty())
	{
		m_ui->tableWidget->clear();
		return;
	}
	//we open the file in ASCII mode
	QFile file(m_filename);
	if (!file.open(QFile::ReadOnly))
	{
		m_ui->tableWidget->clear();
		m_columnType.clear();
		return;
	}
	QTextStream stream(&file);

	//we skip first lines (if needed)
	{
		for (unsigned i = 0; i < m_skippedLines; ++i)
		{
			QString currentLine = stream.readLine();
			//we keep track of the first line
			if (i == 0)
				m_headerLine = currentLine;
		}
	}

	//if the old setup has less than 3 columns, we forget it
	if (m_columnsCount < 3)
	{
		m_ui->tableWidget->clear();
		m_columnType.clear();
		m_columnsCount = 0;
	}
	m_ui->tableWidget->setRowCount(DISPLAYED_LINES + 1);	//+1 for first line shifting

	unsigned lineCount = 0;			//number of lines read
	unsigned totalChars = 0;		//total read characters (for stats)
	unsigned columnsCount = 0;		//max columns count per line
	unsigned commentLines = 0;		//number of comments line skipped

	std::vector<bool> valueIsNumber;	//identifies columns with numbers only [mandatory]
	std::vector<bool> valueIsBelowOne;	//identifies columns with values between -1 and 1 only
	std::vector<bool> valueIsInteger;	//identifies columns with integer values only
	std::vector<bool> valueIsBelow255;	//identifies columns with integer values between 0 and 255 only

	QChar decimalPoint = QLocale().decimalPoint();
	QString currentLine = stream.readLine();
	while (lineCount < LINES_READ_FOR_STATS && !currentLine.isNull())
	{
		//we recognize "//" as the beginning of a comment
		if (!currentLine.startsWith("//")/* || !currentLine.startsWith("#")*/)
		{
			QStringList parts = currentLine.trimmed().split(m_separator, QString::SkipEmptyParts);
			if (lineCount < DISPLAYED_LINES)
			{
				unsigned partsCount = std::min(MAX_COLUMNS, static_cast<unsigned>(parts.size()));
				bool columnCountHasIncreased = (partsCount > columnsCount);

				//do we need to add one or several new columns?
				if (columnCountHasIncreased)
				{
					//we also extend vectors
					for (unsigned i = columnsCount; i < partsCount; ++i)
					{
						valueIsNumber.push_back(true);
						valueIsBelowOne.push_back(true);
						valueIsBelow255.push_back(true);
						valueIsInteger.push_back(true);
					}

					if (m_ui->tableWidget->columnCount() < static_cast<int>(partsCount))
					{
						//DGM: at this stage we must not reduce the table!
						//The first line is sometimes smaller than the next ones
						//and we want to keep the widgets/configuration for the
						//other columns!
						m_ui->tableWidget->setColumnCount(partsCount);
					}
					else if (m_ui->tableWidget->columnCount() > static_cast<int>(partsCount))
					{
						//remove the unnecessary cells!
						for (int i = static_cast<int>(partsCount); i < m_ui->tableWidget->columnCount(); ++i)
						{
							m_ui->tableWidget->setItem(lineCount + 1, i, 0);
						}
					}
					columnsCount = partsCount;
				}

				//we fill the current row with extracted parts
				for (unsigned i = 0; i < partsCount; ++i)
				{
					QTableWidgetItem* newItem = new QTableWidgetItem(parts[i]);

					//test values
					bool isANumber = false;
					double value = parts[i].toDouble(&isANumber);
					if (!isANumber)
					{
						valueIsNumber[i] = false;
						valueIsBelowOne[i] = false;
						valueIsInteger[i] = false;
						valueIsBelow255[i] = false;
						newItem->setBackground(QBrush(QColor(255, 160, 160)));
					}
					else
					{
						if (columnCountHasIncreased || (lineCount == 1 && !valueIsNumber[i]))
						{
							//the previous lines were probably header lines
							//we can forget about their content otherwise it will prevent us from detecting the right pattern
							valueIsNumber[i] = true;
							valueIsBelowOne[i] = true;
							valueIsInteger[i] = true;
							valueIsBelow255[i] = true;
						}
						valueIsBelowOne[i] = valueIsBelowOne[i] && (fabs(value) <= 1.0);
						valueIsInteger[i] = valueIsInteger[i] && !parts[i].contains(decimalPoint);
						valueIsBelow255[i] = valueIsBelow255[i] && valueIsInteger[i] && (value >= 0.0 && value <= 255.0);
					}

					m_ui->tableWidget->setItem(lineCount + 1, i, newItem); //+1 for first line shifting
				}
			}

			totalChars += currentLine.size() + 1; //+1 for return char at eol
			++lineCount;
		}
		else
		{
			if (m_skippedLines == 0 && commentLines == 0)
			{
				//if the very first line is a comment, then we force the user to skip it!
				//this way it will be considered as a header
				m_ui->spinBoxSkipLines->setMinimum(1);
				return;
			}
			++commentLines;
		}

		//read next line
		currentLine = stream.readLine();
	}

	file.close();

	//now we can reduce the table (if necessary)
	if (m_ui->tableWidget->columnCount() > static_cast<int>(columnsCount))
	{
		m_ui->tableWidget->setColumnCount(columnsCount);
	}

	//process header line
	if (!m_headerLine.isEmpty())
	{
		m_headerLine = m_headerLine.trimmed();
		int n = 0;
		while (n < m_headerLine.size() && m_headerLine.at(n) == '/')
		{
			++n;
		}
		if (n != 0)
		{
			m_headerLine.remove(0, n);
		}
		m_ui->headerLabel->setText(QString("Header: ") + m_headerLine);
		m_ui->headerLabel->setVisible(true);
	}
	else
	{
		m_ui->headerLabel->setVisible(false);
	}

	m_ui->commentLinesSkippedLabel->setVisible(commentLines != 0);
	if (commentLines)
	{
		m_ui->commentLinesSkippedLabel->setText(QString("+ %1 comment line(s) skipped").arg(commentLines));
	}

	if (lineCount == 0 || columnsCount == 0)
	{
		m_averageLineSize = -1.0;
		m_ui->tableWidget->clear();
		m_columnType.clear();
		return;
	}

	//average line size
	m_averageLineSize = static_cast<double>(totalChars) / lineCount;
	unsigned approximateTotalLineCount = static_cast<unsigned>(file.size() / m_averageLineSize);

	//we add a type selector for each column
	QStringList propsText;
	{
		propsText.reserve(ASCII_OPEN_DLG_TYPES_COUNT);
		for (unsigned i = 0; i < ASCII_OPEN_DLG_TYPES_COUNT; i++)
		{
			propsText << QString(ASCII_OPEN_DLG_TYPES_NAMES[i]);
		}
	}

	//remove unnecessary columns
	{
		while (columnsCount < m_columnsCount)
			m_ui->tableWidget->removeColumn(--m_columnsCount);
		if (m_columnType.size() > columnsCount)
			m_columnType.resize(columnsCount, UNKNOWN);
		for (unsigned i = lineCount + 1; i <= DISPLAYED_LINES; ++i)
			m_ui->tableWidget->removeRow(i);
	}

	//setup table and widgets
	{
		//Icons
		static const QIcon xIcon		(QString::fromUtf8(":/CC/images/typeXCoordinate.png"));
		static const QIcon yIcon		(QString::fromUtf8(":/CC/images/typeYCoordinate.png"));
		static const QIcon zIcon		(QString::fromUtf8(":/CC/images/typeZCoordinate.png"));
		static const QIcon NormIcon		(QString::fromUtf8(":/CC/images/typeNormal.png"));
		static const QIcon RGBIcon		(QString::fromUtf8(":/CC/images/typeRgbCcolor.png"));
		static const QIcon GreyIcon		(QString::fromUtf8(":/CC/images/typeGrayColor.png"));
		static const QIcon ScalarIcon	(QString::fromUtf8(":/CC/images/typeSF.png"));
		static const QIcon LabelIcon	(QString::fromUtf8(":/CC/images/dbLabelSymbol.png"));

		int columnWidth = (m_ui->tableWidget->width() * 9) / (columnsCount * 10);
		columnWidth = std::max(columnWidth, 80);

		for (unsigned i = 0; i < columnsCount; i++)
		{
			QComboBox* columnHeaderWidget = static_cast<QComboBox*>(m_ui->tableWidget->cellWidget(0, i));
			QComboBox* _columnHeader = columnHeaderWidget;
			if (!columnHeaderWidget)
			{
				columnHeaderWidget = new QComboBox();
				columnHeaderWidget->addItems(propsText);
				columnHeaderWidget->setMaxVisibleItems(ASCII_OPEN_DLG_TYPES_COUNT);
				columnHeaderWidget->setCurrentIndex(0);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_X, xIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Y, yIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Z, zIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NX, NormIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NY, NormIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NZ, NormIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_R, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_G, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_B, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Rf, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Gf, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Bf, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Grey, GreyIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_RGB32i, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_RGB32f, RGBIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Label, LabelIcon);
				columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Scalar, ScalarIcon);

				connect(columnHeaderWidget, SIGNAL(currentIndexChanged(int)), this, SLOT(columnsTypeHasChanged(int)));
			}

			while (m_columnType.size() <= static_cast<size_t>(i))
				m_columnType.push_back(UNKNOWN);
			assert(m_columnType.size() >= static_cast<size_t>(i));

			if (!_columnHeader)
				m_ui->tableWidget->setCellWidget(0, i, columnHeaderWidget);
			m_ui->tableWidget->setColumnWidth(i, columnWidth);

			//a non-numerical column can't be valid
			if (!valueIsNumber[i])
				m_columnType[i] = TEXT;
			else
				// we must do this to ensure we can get a correct result.
				// Otherwise, we may fail in such situations:
				//
				// FILE:
				// Line1   : $ gps file
				// Line2   : $ id name x y z
				// Line3   : 500
				// Line4   : 0    0001.JPG  753811.417453   4307200.381522      1957.803955
				// Linex   : ......
				// Line503 : 499  0500.JPG  753630.672714   4307195.433217      1957.803955
				// 
				// Description:
				// once we open the file, we will get a %m_columnType with 5 values of "TEXT"
				// then if we choose to skip the 3 first lines, we get a %valueIsNumber with 5 "true"
				// but the %m_columnType is still with 5 values of "TEXT" which leads to the failure!
				m_columnType[i] = UNKNOWN;
		}
	}
Example #7
0
	void ItemHandlerCombobox::Handle (const QDomElement& item, QWidget *pwidget)
	{
		QGridLayout *lay = qobject_cast<QGridLayout*> (pwidget->layout ());

		QHBoxLayout *hboxLay = new QHBoxLayout;
		QComboBox *box = new QComboBox (XSD_);

		hboxLay->addWidget (box);

		XSD_->SetTooltip (box, item);
		box->setObjectName (item.attribute ("property"));
		box->setSizeAdjustPolicy (QComboBox::AdjustToContents);
		if (item.hasAttribute ("maxVisibleItems"))
			box->setMaxVisibleItems (item.attribute ("maxVisibleItems").toInt ());

		bool mayHaveDataSource = item.hasAttribute ("mayHaveDataSource") &&
				item.attribute ("mayHaveDataSource").toLower () == "true";
		if (mayHaveDataSource)
		{
			const QString& prop = item.attribute ("property");
			Factory_->RegisterDatasourceSetter (prop,
					[this] (const QString& str, QAbstractItemModel *m, Util::XmlSettingsDialog *xsd)
						{ SetDataSource (str, m, xsd); });
			Propname2Combobox_ [prop] = box;
			Propname2Item_ [prop] = item;
		}

		hboxLay->addStretch ();

		if (item.hasAttribute ("moreThisStuff"))
		{
			QPushButton *moreButt = new QPushButton (tr ("More stuff..."));
			hboxLay->addWidget (moreButt);

			moreButt->setObjectName (item.attribute ("moreThisStuff"));
			connect (moreButt,
					SIGNAL (released ()),
					XSD_,
					SLOT (handleMoreThisStuffRequested ()));
		}

		QDomElement option = item.firstChildElement ("option");
		while (!option.isNull ())
		{
			const auto& images = XSD_->GetImages (option);
			if (!images.isEmpty ())
			{
				const QIcon icon (QPixmap::fromImage (images.at (0)));
				box->addItem (icon,
						XSD_->GetLabel (option),
						option.attribute ("name"));
			}
			else
				box->addItem (XSD_->GetLabel (option),
						option.attribute ("name"));

			auto setColor = [&option, box] (const QString& attr, Qt::ItemDataRole role) -> void
			{
				if (option.hasAttribute (attr))
				{
					const QColor color (option.attribute (attr));
					box->setItemData (box->count () - 1, color, role);
				}
			};
			setColor ("color", Qt::ForegroundRole);
			setColor ("bgcolor", Qt::BackgroundRole);

			option = option.nextSiblingElement ("option");
		}

		connect (box,
				SIGNAL (currentIndexChanged (int)),
				this,
				SLOT (updatePreferences ()));

		QDomElement scriptContainer = item.firstChildElement ("scripts");
		if (!scriptContainer.isNull ())
		{
			Scripter scripter (scriptContainer);

			QStringList fromScript = scripter.GetOptions ();
			Q_FOREACH (const QString& elm, scripter.GetOptions ())
				box->addItem (scripter.HumanReadableOption (elm),
						elm);
		}
Example #8
0
void AsciiOpenDlg::updateTable()
{
    m_ui->tableWidget->setEnabled(false);
    m_ui->extractSFNamesFrom1stLineCheckBox->setEnabled(false);

    bool hadValidHeader = !m_headerLine.isEmpty();
    m_headerLine.clear();

    if (m_filename.isEmpty())
    {
        m_ui->tableWidget->clear();
        return;
    }
    //we open the file in ASCII mode
    QFile file(m_filename);
    if (!file.open(QFile::ReadOnly))
    {
        m_ui->tableWidget->clear();
        m_columnsValidty.clear();
        return;
    }
    QTextStream stream(&file);

    //we skip first lines (if needed)
    {
        for (unsigned i=0; i<m_skippedLines; ++i)
        {
            QString currentLine = stream.readLine();
            //we keep track of the first line
            if (i == 0)
                m_headerLine = currentLine;
        }
    }

    //if the old setup has less than 3 columns, we forget it
    if (m_columnsCount < 3)
    {
        m_ui->tableWidget->clear();
        m_columnsCount = 0;
    }
    m_ui->tableWidget->setRowCount(DISPLAYED_LINES+1);    //+1 for first line shifting

    unsigned lineCount = 0;			//number of lines read
    unsigned totalChars = 0;		//total read characters (for stats)
    unsigned columnsCount = 0;		//max columns count per line
    unsigned commentLines = 0;		//number of comments line skipped

    std::vector<bool> valueIsNumber;	//identifies columns with numbers only [mandatory]
    std::vector<bool> valueIsBelowOne;	//identifies columns with values between -1 and 1 only
    std::vector<bool> valueIsInteger;	//identifies columns with integer values only
    std::vector<bool> valueIsBelow255;	//identifies columns with integer values between 0 and 255 only

    QChar decimalPoint = QLocale().decimalPoint();
    QString currentLine = stream.readLine();
    while (lineCount < LINES_READ_FOR_STATS && !currentLine.isNull())
    {
        //we recognize "//" as the beginning of a comment
        if (!currentLine.startsWith("//"))
        {
            QStringList parts = currentLine.trimmed().split(m_separator,QString::SkipEmptyParts);
            if (lineCount < DISPLAYED_LINES)
            {
                unsigned partsCount = std::min( MAX_COLUMNS, static_cast<unsigned>(parts.size()) );

                //do we need to add one or several new columns?
                if (partsCount > columnsCount)
                {
                    //we also extend vectors
                    for (unsigned i = columnsCount; i < partsCount; ++i)
                    {
                        valueIsNumber.push_back(true);
                        valueIsBelowOne.push_back(true);
                        valueIsBelow255.push_back(true);
                        valueIsInteger.push_back(true);
                    }

                    m_ui->tableWidget->setColumnCount(partsCount);
                    columnsCount = partsCount;
                }

                //we fill the current row with extracted parts
                for (unsigned i = 0; i < partsCount; ++i)
                {
                    QTableWidgetItem *newItem = new QTableWidgetItem(parts[i]);

                    //test values
                    bool isANumber = false;
                    double value = parts[i].toDouble(&isANumber);
                    if (!isANumber)
                    {
                        valueIsNumber[i] = false;
                        valueIsBelowOne[i] = false;
                        valueIsInteger[i] = false;
                        valueIsBelow255[i] = false;
                        newItem->setBackground(QBrush(QColor(255, 160, 160)));
                    }
                    else
                    {
                        valueIsBelowOne[i] = valueIsBelowOne[i] && (fabs(value) <= 1.0);
                        valueIsInteger[i] = valueIsInteger[i] && !parts[i].contains(decimalPoint);
                        valueIsBelow255[i] = valueIsBelow255[i] && valueIsInteger[i] && (value >= 0.0 && value <= 255.0);
                    }

                    m_ui->tableWidget->setItem(lineCount + 1, i, newItem); //+1 for first line shifting
                }
            }

            totalChars += currentLine.size() + 1; //+1 for return char at eol
            ++lineCount;
        }
        else
        {
            if (m_skippedLines == 0 && commentLines == 0)
            {
                //if the very first line is a comment, then we force the user to skip it!
                //this way it will be considered as a header
                m_ui->spinBoxSkipLines->setMinimum(1);
                return;
            }
            ++commentLines;
        }

        //read next line
        currentLine = stream.readLine();
    }

    file.close();

    //process header line
    if (!m_headerLine.isEmpty())
    {
        m_headerLine = m_headerLine.trimmed();
        int n = 0;
        while (n < m_headerLine.size() && m_headerLine.at(n) == '/')
            ++n;
        if (n != 0)
            m_headerLine.remove(0,n);
        m_ui->headerLabel->setText(QString("Header: ")+m_headerLine);
        m_ui->headerLabel->setVisible(true);
    }
    else
    {
        m_ui->headerLabel->setVisible(false);
    }

    m_ui->commentLinesSkippedLabel->setVisible(commentLines != 0);
    if (commentLines)
        m_ui->commentLinesSkippedLabel->setText(QString("+ %1 comment line(s) skipped").arg(commentLines));

    if (lineCount == 0 || columnsCount == 0)
    {
        m_averageLineSize = -1.0;
        m_ui->tableWidget->clear();
        m_columnsValidty.clear();
        return;
    }

    //average line size
    m_averageLineSize = static_cast<double>(totalChars)/lineCount;

    //we add a type selector for each column
    QStringList propsText;
    {
        for (unsigned i=0; i<ASCII_OPEN_DLG_TYPES_NUMBER; i++)
            propsText << QString(ASCII_OPEN_DLG_TYPES_NAMES[i]);
    }

    //remove unnecessary columns
    {
        while (columnsCount < m_columnsCount)
            m_ui->tableWidget->removeColumn(--m_columnsCount);
        if (m_columnsValidty.size() > columnsCount)
            m_columnsValidty.resize(columnsCount);
        for (unsigned i=lineCount+1; i<=DISPLAYED_LINES; ++i)
            m_ui->tableWidget->removeRow(i);
    }

    //setup table and widgets
    {
        //Icons
        static const QIcon xIcon		(QString::fromUtf8(":/CC/images/typeXCoordinate.png"));
        static const QIcon yIcon		(QString::fromUtf8(":/CC/images/typeYCoordinate.png"));
        static const QIcon zIcon		(QString::fromUtf8(":/CC/images/typeZCoordinate.png"));
        static const QIcon NormIcon		(QString::fromUtf8(":/CC/images/typeNormal.png"));
        static const QIcon RGBIcon		(QString::fromUtf8(":/CC/images/typeRgbCcolor.png"));
        static const QIcon GreyIcon		(QString::fromUtf8(":/CC/images/typeGrayColor.png"));
        static const QIcon ScalarIcon	(QString::fromUtf8(":/CC/images/typeSF.png"));

        int columnWidth = (m_ui->tableWidget->width()*9) / (columnsCount*10);
        columnWidth = std::max(columnWidth,80);

        for (unsigned i=0; i<columnsCount; i++)
        {
            QComboBox* columnHeaderWidget = static_cast<QComboBox*>(m_ui->tableWidget->cellWidget(0,i));
            QComboBox* _columnHeader = columnHeaderWidget;
            if (!columnHeaderWidget)
            {
                columnHeaderWidget = new QComboBox();
                columnHeaderWidget->addItems(propsText);
                columnHeaderWidget->setMaxVisibleItems(ASCII_OPEN_DLG_TYPES_NUMBER);
                columnHeaderWidget->setCurrentIndex(0);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_X,xIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Y,yIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Z,zIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NX,NormIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NY,NormIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_NZ,NormIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_R,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_G,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_B,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Rf,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Gf,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Bf,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Grey,GreyIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_Scalar,ScalarIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_RGB32i,RGBIcon);
                columnHeaderWidget->setItemIcon(ASCII_OPEN_DLG_RGB32f,RGBIcon);

                connect(columnHeaderWidget, SIGNAL(currentIndexChanged(int)), this, SLOT(columnsTypeHasChanged(int)));
            }

            while (m_columnsValidty.size() <= static_cast<size_t>(i))
                m_columnsValidty.push_back(false);
            assert(m_columnsValidty.size() >= static_cast<size_t>(i));

            if (!_columnHeader)
                m_ui->tableWidget->setCellWidget(0,i,columnHeaderWidget);
            m_ui->tableWidget->setColumnWidth(i,columnWidth);

            //a non-numerical column can't be valid
            if (!valueIsNumber[i])
                m_columnsValidty[i] = false;
        }
    }
Example #9
0
static mpdm_t kde4_drv_form(mpdm_t a, mpdm_t ctxt)
{
    int n;
    mpdm_t widget_list;
    QWidget *qlist[100];
    mpdm_t r;

    KDialog *dialog = new KDialog(window);

    dialog->setModal(true);
    dialog->setButtons(KDialog::Ok | KDialog::Cancel);

    widget_list = mpdm_aget(a, 0);

    KVBox *vb = new KVBox(dialog);
    dialog->setMainWidget(vb);

    for (n = 0; n < mpdm_size(widget_list); n++) {
        mpdm_t w = mpdm_aget(widget_list, n);
        wchar_t *type;
        mpdm_t t;
        KHBox *hb = new KHBox(vb);

        type = mpdm_string(mpdm_hget_s(w, L"type"));

        if ((t = mpdm_hget_s(w, L"label")) != NULL) {
            QLabel *ql = new QLabel(hb);
            ql->setText(str_to_qstring(mpdm_gettext(t)));
        }

        t = mpdm_hget_s(w, L"value");

        if (wcscmp(type, L"text") == 0) {
            mpdm_t h;
            QComboBox *ql = new QComboBox(hb);

            ql->setEditable(true);
            ql->setMinimumContentsLength(30);
            ql->setMaxVisibleItems(8);

            if (t != NULL)
                ql->setEditText(str_to_qstring(t));

            qlist[n] = ql;

            if ((h = mpdm_hget_s(w, L"history")) != NULL) {
                int i;

                /* has history; fill it */
                h = mp_get_history(h);

                for (i = mpdm_size(h) - 1; i >= 0; i--)
                    ql->addItem(str_to_qstring(mpdm_aget(h, i)));
            }
        }
        else
        if (wcscmp(type, L"password") == 0) {
            QLineEdit *ql = new QLineEdit(hb);

            ql->setEchoMode(QLineEdit::Password);

            qlist[n] = ql;
        }
        else
        if (wcscmp(type, L"checkbox") == 0) {
            QCheckBox *qc = new QCheckBox(hb);

            if (mpdm_ival(t))
                qc->setCheckState(Qt::Checked);

            qlist[n] = qc;
        }
        else
        if (wcscmp(type, L"list") == 0) {
            int i;
            QListWidget *ql = new QListWidget(hb);
            ql->setMinimumWidth(480);

            /* use a monospaced font */
            ql->setFont(QFont(QString("Mono")));

            mpdm_t l = mpdm_hget_s(w, L"list");

            for (i = 0; i < mpdm_size(l); i++)
                ql->addItem(str_to_qstring(mpdm_aget(l, i)));

            ql->setCurrentRow(mpdm_ival(t));

            qlist[n] = ql;
        }

        if (n == 0)
            qlist[n]->setFocus(Qt::OtherFocusReason);
    }

    n = dialog->exec();

    if (!n)
        return NULL;

    r = MPDM_A(mpdm_size(widget_list));

    /* fill the return values */
    for (n = 0; n < mpdm_size(widget_list); n++) {
        mpdm_t w = mpdm_aget(widget_list, n);
        mpdm_t v = NULL;
        wchar_t *type;

        type = mpdm_string(mpdm_hget_s(w, L"type"));

        if (wcscmp(type, L"text") == 0) {
            mpdm_t h;
            QComboBox *ql = (QComboBox *) qlist[n];

            v = qstring_to_str(ql->currentText());

            /* if it has history, add to it */
            if ((h = mpdm_hget_s(w, L"history")) != NULL &&
                v != NULL && mpdm_cmp_s(v, L"") != 0) {
                h = mp_get_history(h);

                if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
                    mpdm_push(h, v);
            }
        }
        else
        if (wcscmp(type, L"password") == 0) {
            QLineEdit *ql = (QLineEdit *) qlist[n];

            v = qstring_to_str(ql->text());
        }
        else
        if (wcscmp(type, L"checkbox") == 0) {
            QCheckBox *qb = (QCheckBox *) qlist[n];

            v = MPDM_I(qb->checkState() == Qt::Checked);
        }
        else
        if (wcscmp(type, L"list") == 0) {
            QListWidget *ql = (QListWidget *) qlist[n];

            v = MPDM_I(ql->currentRow());
        }

        mpdm_aset(r, v, n);
    }

    return r;
}
Example #10
0
QWidget *IrgaDelegate::createEditor(QWidget* parent,
                                     const QStyleOptionViewItem& option,
                                     const QModelIndex& index) const
{
    DEBUG_FUNC_NAME
    Q_UNUSED(option)

    QLabel *label;
    QComboBox *combo;
    QDoubleSpinBox *dspin;
    QLineEdit *ledit;
    QString currentManufacturer = index.model()->data(index.model()->index(IrgaModel::MANUFACTURER, index.column())).toString();
    QString currentModel = index.model()->data(index.model()->index(IrgaModel::MODEL, index.column())).toString();

//    qDebug() << "index.row()" << index.row();

    // can only edit name on blank column
    if (index.column() >= index.model()->columnCount()) return 0;

    // different kind of editor for each row
    switch (index.row())
    {
      case IrgaModel::MANUFACTURER:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          combo->addItems(IrgaDesc::manufacturerStringList());
          combo->view()->setTextElideMode(Qt::ElideNone);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case IrgaModel::MODEL:
          combo = new QComboBox(parent);
          combo->setEditable(false);
          if (currentManufacturer.isEmpty())
          {
              combo->addItems(IrgaDesc::allModelStringList());
          }
          else if (currentManufacturer == IrgaDesc::getIRGA_MANUFACTURER_STRING_0())
          {
              combo->addItems(IrgaDesc::licorModelStringList());
          }
          else if (currentManufacturer == IrgaDesc::getIRGA_MANUFACTURER_STRING_1())
          {
              combo->addItems(IrgaDesc::otherModelStringList());
          }
          combo->view()->setTextElideMode(Qt::ElideNone);
          combo->setMaxVisibleItems(15);
          connect(combo, SIGNAL(activated(int)),
                  this, SLOT(commitAndCloseEditor()));
          return combo;
      case IrgaModel::SWVERSION:
            ledit = new QLineEdit(parent);
            ledit->setPlaceholderText(QStringLiteral("8.0.0"));
            connect(ledit, SIGNAL(editingFinished()),
                    this, SLOT(commitAndCloseEditor()));
            return ledit;
      case IrgaModel::ID:
            ledit = new QLineEdit(parent);
            ledit->setPlaceholderText(QStringLiteral("Alphanumeric ID"));
            connect(ledit, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
            return ledit;
      case IrgaModel::TUBELENGTH:
            if (IrgaDesc::isOpenPathModel(currentModel))
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
                dspin = new QDoubleSpinBox(parent);
                dspin->setDecimals(1);
                dspin->setRange(0.0, 9999.0);
                dspin->setSingleStep(1.0);
                dspin->setAccelerated(true);
                dspin->setSuffix(QStringLiteral(" [cm]"));
                connect(dspin, SIGNAL(editingFinished()),
                      this, SLOT(commitAndCloseEditor()));
                return dspin;
            }
      case IrgaModel::TUBEDIAMETER:
            if (IrgaDesc::isOpenPathModel(currentModel))
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
              dspin = new QDoubleSpinBox(parent);
              dspin->setDecimals(1);
              dspin->setRange(0.0, 99.0);
              dspin->setSingleStep(1.0);
              dspin->setAccelerated(true);
              dspin->setSuffix(QStringLiteral(" [mm]"));
              connect(dspin, SIGNAL(editingFinished()),
                      this, SLOT(commitAndCloseEditor()));
              return dspin;
            }
      case IrgaModel::TUBEFLOWRATE:
            if (IrgaDesc::isOpenPathModel(currentModel))
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
              dspin = new QDoubleSpinBox(parent);
              dspin->setDecimals(1);
              dspin->setRange(0.0, 100.0);
              dspin->setSingleStep(1.0);
              dspin->setAccelerated(true);
              dspin->setSuffix(QStringLiteral(" [l/m]"));
              connect(dspin, SIGNAL(editingFinished()),
                      this, SLOT(commitAndCloseEditor()));
              return dspin;
            }
      case IrgaModel::TUBENSEPARATION:
      case IrgaModel::TUBEESEPARATION:
      case IrgaModel::TUBEVSEPARATION:
          dspin = new QDoubleSpinBox(parent);
          dspin->setDecimals(2);
          dspin->setRange(-1000.0, 1000.0);
          dspin->setSingleStep(1.0);
          dspin->setAccelerated(true);
          dspin->setSuffix(QStringLiteral(" [cm]"));
          connect(dspin, SIGNAL(editingFinished()),
                  this, SLOT(commitAndCloseEditor()));
          return dspin;
        case IrgaModel::VPATHLENGTH:
        case IrgaModel::HPATHLENGTH:
            if (currentModel != IrgaDesc::getIRGA_MODEL_STRING_6()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_7()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_8()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_9()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_10()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_11())
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
                dspin = new QDoubleSpinBox(parent);
                dspin->setDecimals(4);
                dspin->setRange(0.0001, 1000.0);
                dspin->setSingleStep(1.0);
                dspin->setAccelerated(true);
                dspin->setSuffix(QStringLiteral(" [cm]"));
                connect(dspin, SIGNAL(editingFinished()),
                        this, SLOT(commitAndCloseEditor()));
                return dspin;
            }
        case IrgaModel::TAU:
            if (currentModel != IrgaDesc::getIRGA_MODEL_STRING_6()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_7()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_8()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_9()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_10()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_11())
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
                dspin = new QDoubleSpinBox(parent);
                dspin->setDecimals(4);
                dspin->setRange(0.0001, 500.0);
                dspin->setSingleStep(1.0);
                dspin->setAccelerated(true);
                dspin->setSuffix(QStringLiteral(" [s]"));
                connect(dspin, SIGNAL(editingFinished()),
                        this, SLOT(commitAndCloseEditor()));
                return dspin;
            }
        case IrgaModel::KWATER:
        case IrgaModel::KOXYGEN:
            if (currentModel != IrgaDesc::getIRGA_MODEL_STRING_8()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_9()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_10()
                && currentModel != IrgaDesc::getIRGA_MODEL_STRING_11())
            {
                label = new QLabel(parent);
                return label;
            }
            else
            {
                dspin = new QDoubleSpinBox(parent);
                dspin->setDecimals(6);
                dspin->setRange(0.000001, 1.0);
                dspin->setSingleStep(1.0);
                dspin->setAccelerated(true);
                dspin->setSuffix(QStringLiteral(" [") + Defs::M3_G_CM_STRING + QStringLiteral("]"));
                connect(dspin, SIGNAL(editingFinished()),
                        this, SLOT(commitAndCloseEditor()));
                return dspin;
            }
          default:
              return QStyledItemDelegate::createEditor(parent, option, index);
    }
}