mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2026-04-03 18:49:59 +02:00
DiagramContextWidget : The widget is now made with th ui file instead of C++
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@5561 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
180
sources/ui/diagramcontextwidget.cpp
Normal file
180
sources/ui/diagramcontextwidget.cpp
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
Copyright 2006-2018 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include "diagramcontextwidget.h"
|
||||
#include "ui_diagramcontextwidget.h"
|
||||
|
||||
DiagramContextWidget::DiagramContextWidget(QWidget *parent) :
|
||||
QWidget(parent),
|
||||
ui(new Ui::DiagramContextWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
connect(ui->m_table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(checkTableRows()));
|
||||
}
|
||||
|
||||
DiagramContextWidget::~DiagramContextWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::context
|
||||
* @return The diagram context object edited by this widget
|
||||
*/
|
||||
DiagramContext DiagramContextWidget::context() const
|
||||
{
|
||||
DiagramContext context;
|
||||
|
||||
for (int i = 0 ; i < ui->m_table-> rowCount() ; ++ i)
|
||||
{
|
||||
QTableWidgetItem *qtwi_name = ui->m_table-> item(i, 0);
|
||||
QTableWidgetItem *qtwi_value = ui->m_table-> item(i, 1);
|
||||
if (!qtwi_name || !qtwi_value) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString key = qtwi_name -> text();
|
||||
if (key.isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
QString value = qtwi_value -> text();
|
||||
context.addValue(key, value);
|
||||
}
|
||||
|
||||
return(context);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::setContext
|
||||
* Load the content from @context into this widget
|
||||
* @param context
|
||||
*/
|
||||
void DiagramContextWidget::setContext (const DiagramContext &context)
|
||||
{
|
||||
clear();
|
||||
|
||||
int i = 0;
|
||||
for (QString key : context.keys(DiagramContext::Alphabetical))
|
||||
{
|
||||
ui->m_table->setItem(i, 0, new QTableWidgetItem(key));
|
||||
ui->m_table->setItem(i, 1, new QTableWidgetItem(context[key].toString()));
|
||||
++ i;
|
||||
}
|
||||
|
||||
checkTableRows();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::nameLessRowsCount
|
||||
* @return The count of name-less rows in the table
|
||||
*/
|
||||
int DiagramContextWidget::nameLessRowsCount() const
|
||||
{
|
||||
int name_less_rows_count = 0;
|
||||
for (int i = 0 ; i < ui->m_table->rowCount() ; ++ i)
|
||||
{
|
||||
QTableWidgetItem *qtwi_name = ui->m_table->item(i, 0);
|
||||
if (qtwi_name && qtwi_name -> text().isEmpty()) {
|
||||
++ name_less_rows_count;
|
||||
}
|
||||
}
|
||||
|
||||
return(name_less_rows_count);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::clear
|
||||
* Clear any values entered within this widget
|
||||
*/
|
||||
void DiagramContextWidget::clear()
|
||||
{
|
||||
ui->m_table->clearContents();
|
||||
for (int i = 1 ; i < ui->m_table->rowCount() ; ++ i) {
|
||||
ui->m_table->removeRow(i);
|
||||
}
|
||||
|
||||
refreshFormatLabel();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::highlightNonAcceptableKeys
|
||||
* Highlight keys that would not be accepted by a DiagramContext object.
|
||||
* @return the number of highlighted keys.
|
||||
*/
|
||||
int DiagramContextWidget::highlightNonAcceptableKeys()
|
||||
{
|
||||
static QRegExp re(DiagramContext::validKeyRegExp());
|
||||
|
||||
QBrush fg_brush = ui->m_table->palette().brush(QPalette::WindowText);
|
||||
|
||||
int invalid_keys = 0;
|
||||
for (int i = 0 ; i < ui->m_table->rowCount() ; ++ i)
|
||||
{
|
||||
QTableWidgetItem *qtwi_name = ui->m_table->item(i, 0);
|
||||
if (!qtwi_name) {
|
||||
continue;
|
||||
}
|
||||
|
||||
bool highlight = false;
|
||||
if (!qtwi_name -> text().isEmpty())
|
||||
{
|
||||
if (!re.exactMatch(qtwi_name -> text()))
|
||||
{
|
||||
highlight = true;
|
||||
++ invalid_keys;
|
||||
}
|
||||
}
|
||||
qtwi_name -> setForeground(highlight ? Qt::red : fg_brush);
|
||||
}
|
||||
|
||||
return(invalid_keys);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::refreshFormatLabel
|
||||
* Sets the text describing the acceptable format for keys when adding extra
|
||||
* key/value pairs.
|
||||
*/
|
||||
void DiagramContextWidget::refreshFormatLabel()
|
||||
{
|
||||
QString format_text = tr(
|
||||
"Les noms ne peuvent contenir que des lettres minuscules, des "
|
||||
"chiffres et des tirets."
|
||||
);
|
||||
|
||||
if (highlightNonAcceptableKeys()) {
|
||||
format_text = QString("<span style=\"color: red;\">%1</span>").arg(format_text);
|
||||
}
|
||||
ui->m_label->setText(format_text);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DiagramContextWidget::checkTableRows
|
||||
* Adds a row in the additional fields table if needed
|
||||
*/
|
||||
void DiagramContextWidget::checkTableRows()
|
||||
{
|
||||
refreshFormatLabel();
|
||||
if (!nameLessRowsCount())
|
||||
{
|
||||
int new_idx = ui->m_table->rowCount();
|
||||
ui->m_table->setRowCount(new_idx + 1);
|
||||
ui->m_table->setItem(new_idx, 0, new QTableWidgetItem(""));
|
||||
ui->m_table->setItem(new_idx, 1, new QTableWidgetItem(""));
|
||||
}
|
||||
}
|
||||
52
sources/ui/diagramcontextwidget.h
Normal file
52
sources/ui/diagramcontextwidget.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
Copyright 2006-2018 The QElectroTech Team
|
||||
This file is part of QElectroTech.
|
||||
|
||||
QElectroTech is free software: you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation, either version 2 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
QElectroTech is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#ifndef DIAGRAMCONTEXTWIDGET_H
|
||||
#define DIAGRAMCONTEXTWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
#include "diagramcontext.h"
|
||||
|
||||
namespace Ui {
|
||||
class DiagramContextWidget;
|
||||
}
|
||||
|
||||
class DiagramContextWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit DiagramContextWidget(QWidget *parent = nullptr);
|
||||
~DiagramContextWidget();
|
||||
|
||||
DiagramContext context() const;
|
||||
void setContext (const DiagramContext &context);
|
||||
int nameLessRowsCount() const;
|
||||
|
||||
public slots:
|
||||
void clear();
|
||||
int highlightNonAcceptableKeys();
|
||||
void refreshFormatLabel();
|
||||
|
||||
private slots:
|
||||
void checkTableRows();
|
||||
|
||||
private:
|
||||
Ui::DiagramContextWidget *ui;
|
||||
};
|
||||
|
||||
#endif // DIAGRAMCONTEXTWIDGET_H
|
||||
64
sources/ui/diagramcontextwidget.ui
Normal file
64
sources/ui/diagramcontextwidget.ui
Normal file
@@ -0,0 +1,64 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>DiagramContextWidget</class>
|
||||
<widget class="QWidget" name="DiagramContextWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>400</width>
|
||||
<height>300</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<layout class="QVBoxLayout" name="verticalLayout">
|
||||
<item>
|
||||
<widget class="QLabel" name="m_label">
|
||||
<property name="text">
|
||||
<string>Les noms ne peuvent contenir que des lettres minuscules, des chiffres et des tirets.</string>
|
||||
</property>
|
||||
<property name="wordWrap">
|
||||
<bool>true</bool>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QTableWidget" name="m_table">
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::SingleSelection</enum>
|
||||
</property>
|
||||
<property name="rowCount">
|
||||
<number>1</number>
|
||||
</property>
|
||||
<property name="columnCount">
|
||||
<number>2</number>
|
||||
</property>
|
||||
<attribute name="horizontalHeaderVisible">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
<attribute name="horizontalHeaderStretchLastSection">
|
||||
<bool>true</bool>
|
||||
</attribute>
|
||||
<attribute name="verticalHeaderVisible">
|
||||
<bool>false</bool>
|
||||
</attribute>
|
||||
<row/>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Nom</string>
|
||||
</property>
|
||||
</column>
|
||||
<column>
|
||||
<property name="text">
|
||||
<string>Valeur</string>
|
||||
</property>
|
||||
</column>
|
||||
</widget>
|
||||
</item>
|
||||
</layout>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
Reference in New Issue
Block a user