Ejemplo n.º 1
0
QWidget *BackgroundAction::createWidget(QWidget *parent)
{
    QComboBox *comboBox = new QComboBox(parent);
    comboBox->setFixedWidth(42);

    for (int i = 0; i < colors().count(); ++i) {
        comboBox->addItem(tr(""));
        comboBox->setItemIcon(i, iconForColor((colors().at(i))));
    }

    comboBox->setCurrentIndex(0);
    connect(comboBox, SIGNAL(currentIndexChanged(int)), SLOT(emitBackgroundChanged(int)));

    comboBox->setProperty("hideborder", true);
    return comboBox;
}
Ejemplo n.º 2
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);
    }
Ejemplo n.º 3
0
/**
  * Updates the icon combobox depending on user's selection
  */
void OptionsDialog::updateIconComboBox(int index) {
    if (isRemIns == true)
        return;

    QString objName = QObject::sender()->objectName();

    if (objName.endsWith("2")) {
        p2Icon += index;
        return;
    }

    QComboBox *cBoxR = combop2;

    if (iconType == 0) {
        if (index == 0) {
            cBoxR->setItemIcon(0, *classicIcons[1]);
           cBoxR->setItemText(0, tr("White"));
           p2Icon = 0;
        }
        else {
            cBoxR->setItemIcon(0, *classicIcons[0]);
            cBoxR->setItemText(0, tr("Black"));
            p2Icon = 1;
        }
    }
    else if (iconType == 1) {
        if (index == 0) {
            cBoxR->setItemIcon(0, *osIcons[1]);
            cBoxR->setItemText(0, tr("Debian"));
            cBoxR->setItemIcon(1, *osIcons[2]);
            cBoxR->setItemText(1, tr("MacOS"));
            cBoxR->setItemIcon(2, *osIcons[3]);
            cBoxR->setItemText(2, tr("Windows"));
            p2Icon = 1;
        } else if (index == 1) {
            cBoxR->setItemIcon(0, *osIcons[0]);
            cBoxR->setItemText(0, tr("ArchLinux"));
            cBoxR->setItemIcon(1, *osIcons[2]);
            cBoxR->setItemText(1, tr("MacOS"));
            cBoxR->setItemIcon(2, *osIcons[3]);
            cBoxR->setItemText(2, tr("Windows"));
            p2Icon = 0;
        } else if (index == 2) {
            cBoxR->setItemIcon(0, *osIcons[0]);
            cBoxR->setItemText(0, tr("ArchLinux"));
            cBoxR->setItemIcon(1, *osIcons[1]);
            cBoxR->setItemText(1, tr("Debian"));
            cBoxR->setItemIcon(2, *osIcons[3]);
            cBoxR->setItemText(2, tr("Windows"));
             p2Icon = 0;
        } else {
            cBoxR->setItemIcon(0, *osIcons[0]);
            cBoxR->setItemText(0, tr("ArchLinux"));
            cBoxR->setItemIcon(1, *osIcons[1]);
            cBoxR->setItemText(1, tr("Debian"));
            cBoxR->setItemIcon(2, *osIcons[2]);
            cBoxR->setItemText(2, tr("MacOS"));
             p2Icon = 0;
        }
    } else if (iconType == 2) {
        if (index == 0) {
            cBoxR->setItemIcon(0, *progIcons[1]);
            cBoxR->setItemText(0, tr("Java"));
            cBoxR->setItemIcon(1, *progIcons[2]);
            cBoxR->setItemText(1, tr("Qt"));
             p2Icon = 1;
        } else if (index == 1) {
            cBoxR->setItemIcon(0, *progIcons[0]);
            cBoxR->setItemText(0, tr("C"));
            cBoxR->setItemIcon(1, *progIcons[2]);
            cBoxR->setItemText(1, tr("Qt"));
             p2Icon = 0;
        } else {
            cBoxR->setItemIcon(0, *progIcons[0]);
            cBoxR->setItemText(0, tr("C"));
            cBoxR->setItemIcon(1, *progIcons[1]);
            cBoxR->setItemText(1, tr("Java"));
             p2Icon = 0;
        }
    } else {
        if (index == 0) {
            cBoxR->setItemIcon(0, *teamIcons[1]);
            cBoxR->setItemText(0, tr("OSFP"));
            cBoxR->setItemIcon(1, *teamIcons[2]);
            cBoxR->setItemText(1, tr("PAO"));
             p2Icon = 1;
        } else if (index == 1) {
            cBoxR->setItemIcon(0, *teamIcons[0]);
            cBoxR->setItemText(0, tr("AEK"));
            cBoxR->setItemIcon(1, *teamIcons[2]);
            cBoxR->setItemText(1, tr("PAO"));
             p2Icon = 0;
        } else {
            cBoxR->setItemIcon(0, *teamIcons[0]);
            cBoxR->setItemText(0, tr("AEK"));
            cBoxR->setItemIcon(1, *teamIcons[1]);
            cBoxR->setItemText(1, tr("OSFP"));
             p2Icon = 0;
        }
    }
    p1Icon = index;
}
Ejemplo n.º 4
0
MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
	ui->setupUi(this);
#if !MAINWINDOW_ENABLE_UNDO
	delete ui->actionUndo;
#endif
	ui->optionsWidget->setVisible(ui->optionsCheckBox->isChecked());
	setWindowTitle(APP_NAME);

	playerSelector = new PlayerSelector(&tileFactory, this);

	for (int i = 1; i <= MAX_PLAYERS; ++i)
	{
		QLabel * numberLabel = new QLabel(QString::number(i));
		ui->ngPlayerLayout->addWidget(numberLabel, i, 0);

		QComboBox * colorBox = new QComboBox();
		for (uint j = 0; j < colors.size(); ++j)
		{
			colorBox->addItem(colorNames[j]);

			QPixmap px(32, 32);
			QPainter painter(&px);
			painter.fillRect(px.rect(), colors[j]);
			painter.drawRect(0, 0, px.width() - 1, px.height() - 1);
			colorBox->setItemIcon(j, QIcon(px));
		}
		colorBox->setCurrentIndex(i-1);
		ui->ngPlayerLayout->addWidget(colorBox, i, 1);
		connect(colorBox, SIGNAL(currentIndexChanged(int)), this, SLOT(colorBoxChanged(int)));

		QComboBox * typeBox = new QComboBox();
		typeBox->addItems(QStringList{tr("", "player selection: no player"), tr("Player"), tr("Computer")});
		ui->ngPlayerLayout->addWidget(typeBox, i, 2);
		connect(typeBox, SIGNAL(currentIndexChanged(int)), this, SLOT(typeBoxChanged(int)));

		QLineEdit * nameEdit = new QLineEdit();
		ui->ngPlayerLayout->addWidget(nameEdit, i, 3);

		ngPlayerEdits[i-1] = NgPlayerEdit{colorBox, typeBox, nameEdit};
	}

	auto actionGroup = new QActionGroup(this);
	actionGroup->addAction(ui->actionRandom_Tiles);
	actionGroup->addAction(ui->actionChoose_Tiles);

	boardUi = new BoardGraphicsScene(&tileFactory, &imgFactory, ui->boardView);
	game = new Game(this, true);
	game->addView(this);
	gameThread = new GameThread(game, this);

	boardUi->setGame(game);
	ui->boardView->setScene(boardUi);

	connect(boardUi, SIGNAL(sceneRectChanged(QRectF)), this, SLOT(recenter(QRectF)));
	connect(this, SIGNAL(gameEvent(QString)), this, SLOT(displayGameEvent(QString)));
	connect(this, SIGNAL(gameEventPop()), this, SLOT(displayGameEventPop()));

	readSettings();

	game->addWatchingPlayer(this);
}
Ejemplo n.º 5
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;
		}
	}
Ejemplo n.º 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_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;
        }
    }