mirror of
https://github.com/qelectrotech/qelectrotech-source-mirror.git
synced 2025-12-17 20:50:34 +01:00
Title block properties: now warn users when they enter invalid keys.
git-svn-id: svn+ssh://svn.tuxfamily.org/svnroot/qet/qet/trunk@1780 bfdf4180-ca20-0410-9c96-a3a8aa849046
This commit is contained in:
@@ -63,11 +63,19 @@ bool DiagramContext::operator!=(const DiagramContext &dc) const {
|
||||
return(!(*this == dc));
|
||||
}
|
||||
|
||||
/**
|
||||
@return the regular expression used to check whether a given key is acceptable.
|
||||
@see keyIsAcceptable()
|
||||
*/
|
||||
QString DiagramContext::validKeyRegExp() {
|
||||
return("^[a-z0-9-]+$");
|
||||
}
|
||||
|
||||
/**
|
||||
@param key a key string
|
||||
@return true if that key is acceptable, false otherwise
|
||||
*/
|
||||
bool DiagramContext::keyIsAcceptable(const QString &key) const {
|
||||
static QRegExp re("^[a-z0-9-]+$");
|
||||
static QRegExp re(DiagramContext::validKeyRegExp());
|
||||
return(re.exactMatch(key));
|
||||
}
|
||||
|
||||
@@ -35,6 +35,8 @@ class DiagramContext {
|
||||
bool operator==(const DiagramContext &) const;
|
||||
bool operator!=(const DiagramContext &) const;
|
||||
|
||||
static QString validKeyRegExp();
|
||||
|
||||
private:
|
||||
bool keyIsAcceptable(const QString &) const;
|
||||
/// Diagram context data (key/value pairs)
|
||||
|
||||
@@ -216,10 +216,27 @@ void TitleBlockPropertiesWidget::setCurrentTitleBlockTemplateName(const QString
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
Sets the text describing the acceptable format for keys when adding extra
|
||||
key/value pairs.
|
||||
*/
|
||||
void TitleBlockPropertiesWidget::refreshFieldsFormatLabel() {
|
||||
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);
|
||||
}
|
||||
additional_fields_format_label -> setText(format_text);
|
||||
}
|
||||
|
||||
/**
|
||||
Adds a row in the additional fields table if needed.
|
||||
*/
|
||||
void TitleBlockPropertiesWidget::checkTableRows() {
|
||||
refreshFieldsFormatLabel();
|
||||
if (!nameLessRowsCount()) {
|
||||
int new_idx = additional_fields_table -> rowCount();
|
||||
additional_fields_table -> setRowCount(new_idx + 1);
|
||||
@@ -327,6 +344,10 @@ void TitleBlockPropertiesWidget::initWidgets(const TitleBlockProperties &titlebl
|
||||
);
|
||||
additional_fields_label -> setWordWrap(true);
|
||||
additional_fields_label -> setAlignment(Qt::AlignJustify);
|
||||
additional_fields_format_label = new QLabel();
|
||||
additional_fields_format_label -> setWordWrap(true);
|
||||
additional_fields_format_label -> setAlignment(Qt::AlignJustify);
|
||||
|
||||
int num_rows = titleblock.context.keys().count() + 1;
|
||||
additional_fields_table = new QTableWidget(num_rows, 2);
|
||||
additional_fields_table -> setHorizontalHeaderLabels(QStringList() << tr("Nom") << tr("Valeur"));
|
||||
@@ -339,6 +360,7 @@ void TitleBlockPropertiesWidget::initWidgets(const TitleBlockProperties &titlebl
|
||||
++ i;
|
||||
}
|
||||
|
||||
refreshFieldsFormatLabel();
|
||||
connect(additional_fields_table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(checkTableRows()));
|
||||
|
||||
tabbar = new QTabBar(this);
|
||||
@@ -380,6 +402,7 @@ void TitleBlockPropertiesWidget::initLayouts() {
|
||||
QWidget *widget_user_fields = new QWidget(this);
|
||||
QVBoxLayout *layout_user_fields = new QVBoxLayout(widget_user_fields);
|
||||
layout_user_fields -> addWidget(additional_fields_label);
|
||||
layout_user_fields -> addWidget(additional_fields_format_label);
|
||||
layout_user_fields -> addWidget(additional_fields_table);
|
||||
layout_user_fields -> setContentsMargins(0, 0, 0, 0);
|
||||
|
||||
@@ -427,3 +450,28 @@ int TitleBlockPropertiesWidget::nameLessRowsCount() const {
|
||||
}
|
||||
return(name_less_rows_count);
|
||||
}
|
||||
|
||||
/**
|
||||
Highlight keys that would not be accepted by a DiagramContext object.
|
||||
@return the number of highlighted keys.
|
||||
*/
|
||||
int TitleBlockPropertiesWidget::highlightNonAcceptableKeys() {
|
||||
static QRegExp re(DiagramContext::validKeyRegExp());
|
||||
|
||||
QBrush fg_brush = additional_fields_table -> palette().brush(QPalette::WindowText);
|
||||
|
||||
int invalid_keys = 0;
|
||||
for (int i = 0 ; i < additional_fields_table -> rowCount() ; ++ i) {
|
||||
QTableWidgetItem *qtwi_name = additional_fields_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);
|
||||
}
|
||||
|
||||
@@ -48,6 +48,7 @@ class TitleBlockPropertiesWidget : public QWidget {
|
||||
|
||||
// slots:
|
||||
private slots:
|
||||
void refreshFieldsFormatLabel();
|
||||
void checkTableRows();
|
||||
void updateTemplateList();
|
||||
void editCurrentTitleBlockTemplate();
|
||||
@@ -58,6 +59,7 @@ class TitleBlockPropertiesWidget : public QWidget {
|
||||
void initWidgets(const TitleBlockProperties &);
|
||||
void initLayouts();
|
||||
int nameLessRowsCount() const;
|
||||
int highlightNonAcceptableKeys();
|
||||
|
||||
signals:
|
||||
void editTitleBlockTemplate(const QString &, bool);
|
||||
@@ -82,6 +84,7 @@ class TitleBlockPropertiesWidget : public QWidget {
|
||||
QRadioButton *titleblock_fixed_date;
|
||||
bool display_current_date;
|
||||
QLabel *additional_fields_label;
|
||||
QLabel *additional_fields_format_label;
|
||||
QTableWidget *additional_fields_table;
|
||||
QTabBar *tabbar;
|
||||
TitleBlockTemplatesCollection *tbt_collection_;
|
||||
|
||||
Reference in New Issue
Block a user