[qet] [1780] Title block properties: now warn users when they enter invalid keys.

[ Thread Index | Date Index | More lists.tuxfamily.org/qet Archives ]


Revision: 1780
Author:   xavier
Date:     2012-05-09 19:21:46 +0200 (Wed, 09 May 2012)
Log Message:
-----------
Title block properties: now warn users when they enter invalid keys.

Modified Paths:
--------------
    trunk/sources/diagramcontext.cpp
    trunk/sources/diagramcontext.h
    trunk/sources/titleblockpropertieswidget.cpp
    trunk/sources/titleblockpropertieswidget.h

Modified: trunk/sources/diagramcontext.cpp
===================================================================
--- trunk/sources/diagramcontext.cpp	2012-05-09 17:21:44 UTC (rev 1779)
+++ trunk/sources/diagramcontext.cpp	2012-05-09 17:21:46 UTC (rev 1780)
@@ -64,10 +64,18 @@
 }
 
 /**
+	@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));
 }

Modified: trunk/sources/diagramcontext.h
===================================================================
--- trunk/sources/diagramcontext.h	2012-05-09 17:21:44 UTC (rev 1779)
+++ trunk/sources/diagramcontext.h	2012-05-09 17:21:46 UTC (rev 1780)
@@ -35,6 +35,8 @@
 	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)

Modified: trunk/sources/titleblockpropertieswidget.cpp
===================================================================
--- trunk/sources/titleblockpropertieswidget.cpp	2012-05-09 17:21:44 UTC (rev 1779)
+++ trunk/sources/titleblockpropertieswidget.cpp	2012-05-09 17:21:46 UTC (rev 1780)
@@ -217,9 +217,26 @@
 }
 
 /**
+	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 @@
 	);
 	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 @@
 		++ i;
 	}
 	
+	refreshFieldsFormatLabel();
 	connect(additional_fields_table, SIGNAL(itemChanged(QTableWidgetItem *)), this, SLOT(checkTableRows()));
 	
 	tabbar = new QTabBar(this);
@@ -380,6 +402,7 @@
 	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 @@
 	}
 	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);
+}

Modified: trunk/sources/titleblockpropertieswidget.h
===================================================================
--- trunk/sources/titleblockpropertieswidget.h	2012-05-09 17:21:44 UTC (rev 1779)
+++ trunk/sources/titleblockpropertieswidget.h	2012-05-09 17:21:46 UTC (rev 1780)
@@ -48,6 +48,7 @@
 	
 	// slots:
 	private slots:
+	void refreshFieldsFormatLabel();
 	void checkTableRows();
 	void updateTemplateList();
 	void editCurrentTitleBlockTemplate();
@@ -58,6 +59,7 @@
 	void initWidgets(const TitleBlockProperties &);
 	void initLayouts();
 	int nameLessRowsCount() const;
+	int highlightNonAcceptableKeys();
 	
 	signals:
 	void editTitleBlockTemplate(const QString &, bool);
@@ -82,6 +84,7 @@
 	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_;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/