From 74df18dda93f154339af1137e8676337233b4a61 Mon Sep 17 00:00:00 2001
From: Kevin Smith <git@kismith.co.uk>
Date: Fri, 27 Apr 2012 17:02:10 +0100
Subject: Don't allow editing of AdHoc result forms.

Resolves: #1090

diff --git a/Swift/QtUI/QtAdHocCommandWindow.cpp b/Swift/QtUI/QtAdHocCommandWindow.cpp
index b148560..88aa708 100644
--- a/Swift/QtUI/QtAdHocCommandWindow.cpp
+++ b/Swift/QtUI/QtAdHocCommandWindow.cpp
@@ -15,7 +15,7 @@ const int FormLayoutIndex = 1;
 
 namespace Swift {
 QtAdHocCommandWindow::QtAdHocCommandWindow(boost::shared_ptr<OutgoingAdHocCommandSession> command) : command_(command) {
-
+	someActions_ = false;
 	formWidget_ = NULL;
 
 	setAttribute(Qt::WA_DeleteOnClose);
@@ -109,16 +109,19 @@ void QtAdHocCommandWindow::setForm(Form::ref form) {
 	formWidget_ = new QtFormWidget(form, this);
 	layout_->insertWidget(FormLayoutIndex, formWidget_);
 	show();
+	formWidget_->setEditable(someActions_);
 }
 
 void QtAdHocCommandWindow::setNoForm() {
 	delete formWidget_;
+	formWidget_ = NULL;
 	show();
 }
 
 typedef std::pair<Command::Action, QPushButton*> ActionButton;
 
 void QtAdHocCommandWindow::setAvailableActions(Command::ref /*commandResult*/) {
+	someActions_ = false;
 	foreach (ActionButton pair, actions_) {
 		OutgoingAdHocCommandSession::ActionState state = command_->getActionState(pair.first);
 		if (state & OutgoingAdHocCommandSession::Present) {
@@ -129,11 +132,15 @@ void QtAdHocCommandWindow::setAvailableActions(Command::ref /*commandResult*/) {
 		}
 		if (state & OutgoingAdHocCommandSession::Enabled) {
 			pair.second->setEnabled(true);
+			someActions_ = true;
 		}
 		else {
 			pair.second->setEnabled(false);
 		}
 	}
+	if (formWidget_) {
+		formWidget_->setEditable(someActions_);
+	}
 }
 
 }
diff --git a/Swift/QtUI/QtAdHocCommandWindow.h b/Swift/QtUI/QtAdHocCommandWindow.h
index 42e3b1c..7f824f8 100644
--- a/Swift/QtUI/QtAdHocCommandWindow.h
+++ b/Swift/QtUI/QtAdHocCommandWindow.h
@@ -44,5 +44,6 @@ namespace Swift {
 			QPushButton* cancelButton_;
 			std::map<Command::Action, QPushButton*> actions_;
 			QBoxLayout* layout_;
+			bool someActions_;
 	};
 }
diff --git a/Swift/QtUI/QtFormWidget.cpp b/Swift/QtUI/QtFormWidget.cpp
index 2df8d7f..158bc9d 100644
--- a/Swift/QtUI/QtFormWidget.cpp
+++ b/Swift/QtUI/QtFormWidget.cpp
@@ -241,4 +241,34 @@ Form::ref QtFormWidget::getCompletedForm() {
 	return result;
 }
 
+template<class T> void QtFormWidget::setEnabled(QWidget* rawWidget, bool editable) {
+	T* widget = qobject_cast<T*>(rawWidget);
+	if (widget) {
+		widget->setEnabled(editable);
+	}
+}
+
+template<class T> void QtFormWidget::setEditable(QWidget* rawWidget, bool editable) {
+	T* widget = qobject_cast<T*>(rawWidget);
+	if (widget) {
+		widget->setReadOnly(!editable);
+	}
+}
+
+void QtFormWidget::setEditable(bool editable) {
+	if (!form_) {
+		return;
+	}
+	foreach (boost::shared_ptr<FormField> field, form_->getFields()) {
+		QWidget* widget = NULL;
+		if (field) {
+			widget = fields_[field->getName()];
+		}
+		setEnabled<QCheckBox>(widget, editable);
+		setEnabled<QListWidget>(widget, editable);
+		setEditable<QTextEdit>(widget, editable);
+		setEditable<QLineEdit>(widget, editable);
+	}
+}
+
 }
diff --git a/Swift/QtUI/QtFormWidget.h b/Swift/QtUI/QtFormWidget.h
index 2fb7b98..c7aae73 100644
--- a/Swift/QtUI/QtFormWidget.h
+++ b/Swift/QtUI/QtFormWidget.h
@@ -21,9 +21,12 @@ class QtFormWidget : public QWidget {
 		QtFormWidget(Form::ref form, QWidget* parent = NULL);
 		virtual ~QtFormWidget();
 		Form::ref getCompletedForm();
+		void setEditable(bool editable);
 	private:
 		QWidget* createWidget(FormField::ref field);
 		QListWidget* createList(FormField::ref field);
+		template<class T> void setEnabled(QWidget* rawWidget, bool editable);
+		template<class T> void setEditable(QWidget* rawWidget, bool editable);
 		std::map<std::string, QWidget*> fields_;
 		Form::ref form_;
 };
-- 
cgit v0.10.2-6-g49f6