Andreas Tscharner avatar Andreas Tscharner committed 3aaf687

Move edit dialog logic into its own class

Comments (0)

Files changed (5)

src/gui/ButtonHandlers.cxx

 #include <gtkmm/stock.h>
 #include <gtkmm/treeview.h>
 #include "ButtonHandlers.hxx"
-#include "../include/CompressDecompress.hxx"
-#include "../include/EncryptDecrypt.hxx"
+#include "EditDialog.hxx"
 
 
-void ButtonHandlers::setupEditDialog(Gtk::TreeModel::Row p_row)
-{
-	Gtk::Entry *edResID;
-	Gtk::Entry *edResFilename;
-	Gtk::Entry *edResPassword;
-	Glib::ustring compTypeName;
-	Glib::ustring encTypeName;
-
-
-	if (!m_editDialog) {
-		return;
-	};
-
-	m_refBuilder->get_widget("edID", edResID);
-	edResID->set_text(p_row[m_resColModel->FUniqueResID]);
-
-	m_refBuilder->get_widget("edFilename", edResFilename);
-	edResFilename->set_text(p_row[m_resColModel->FResFilename]);
-
-	compTypeName = p_row[m_resColModel->FCompressTypeName];
-	m_compSelect->set_active(this->compNameToIndex(compTypeName.c_str()));
-
-	encTypeName = p_row[m_resColModel->FEncryptTypeName];
-	m_encSelect->set_active(this->encNameToIndex(encTypeName.c_str()));
-
-	m_refBuilder->get_widget("edPassword", edResPassword);
-	edResPassword->set_text(p_row[m_resColModel->FEncPassword]);
-}
-
-inline int ButtonHandlers::compNameToIndex(const char *p_compName)
-{
-	if (strcmp("None", p_compName) == 0) {
-		return 0;
-	};
-
-	for (int iter = lrc::NoneCompression; iter < lrc::lastCompression; iter++) {
-		if (strcmp(lrc::CompressionTypeNames[iter], p_compName) == 0) {
-			return iter;
-		};
-	};
-
-	return 0;
-}
-
-inline int ButtonHandlers::encNameToIndex(const char *p_encName)
-{
-	if (strcmp("None", p_encName) == 0) {
-		return 0;
-	};
-
-	for (int iter = lrc::NoneEncryption; iter < lrc::lastEncryption; iter++) {
-		if (strcmp(lrc::EncryptionTypeNames[iter], p_encName) == 0) {
-			return iter;
-		};
-	};
-
-	return 0;
-}
-
 void ButtonHandlers::on_add_button_clicked(void)
 {
 	int dlgRet;
 
 	for (auto eachPath : selectedRows) {
 		currentRow = m_listHandler->getResourceEntry(eachPath);
-		this->setupEditDialog(currentRow);
-		dlgRet = m_editDialog->run();
-		m_editDialog->hide();
+		dlgRet = m_editDlg->show_dialog(currentRow);
 	};
 }
 
 
 
 	m_refBuilder = p_refBuilder;
-	m_resColModel = p_resColModel;
 
 	p_refBuilder->get_widget("bnAdd", m_addButton);
 	m_addButton->signal_clicked().connect(sigc::mem_fun(*this,
 	anyFile->add_pattern("*");
 	m_addFilesDlg->add_filter(anyFile);
 
-	p_refBuilder->get_widget("dlgEdit", m_editDialog);
+	m_listHandler = nullptr;
 
-	p_refBuilder->get_widget("cbxCompression", m_compSelect);
-	for (unsigned int iter = lrc::NoneCompression; iter < lrc::lastCompression; iter++) {
-		m_compSelect->append(lrc::CompressionTypeNames[iter]);
-	};
-	m_compSelect->set_active(0);
-
-	p_refBuilder->get_widget("cbxEncryption", m_encSelect);
-	for (unsigned int iter = lrc::NoneEncryption; iter < lrc::lastEncryption; iter++) {
-		m_encSelect->append(lrc::EncryptionTypeNames[iter]);
-	};
-	m_encSelect->set_active(0);
-
-	m_listHandler = nullptr;
+	m_editDlg = new EditDialog(p_refBuilder, p_resColModel);
 }
 
 ButtonHandlers::~ButtonHandlers(void)
 {
-	delete m_encSelect;
-	delete m_compSelect;
-	delete m_editDialog;
+	delete m_editDlg;
 	delete m_addFilesDlg;
 	delete m_editButton;
 	delete m_removeButton;

src/gui/ButtonHandlers.hxx

  * on the main window
  *
  * \author Andreas Tscharner
- * \date 2013-03-10
+ * \date 2013-07-10
  */
 
 
 #include <gtkmm/button.h>
 #include <gtkmm/builder.h>
 #include <gtkmm/filechooserdialog.h>
-#include <gtkmm/dialog.h>
-#include <gtkmm/comboboxtext.h>
 #include "ListHandler.hxx"
 #include "ResColumnModel.hxx"
+#include "EditDialog.hxx"
 
 
 /*! \class ButtonHandlers
 		Gtk::Button *m_removeButton;   //!< Reference to "Remove" button
 		Gtk::Button *m_editButton;     //!< Reference to "Edit" button
 		Gtk::FileChooserDialog *m_addFilesDlg;   //!< Reference to "Add Files" dialog
-		Gtk::Dialog *m_editDialog;     //!< Reference to dialog for editing the selected resource
-		Gtk::ComboBoxText *m_compSelect;    //!< Reference to compression combobox
-		Gtk::ComboBoxText *m_encSelect;     //!< Reference to encryption combobox
 		ListHandler *m_listHandler;    //!< Reference to (global) list handler class
-		ResColumnModel *m_resColModel; //!< Reference to instance of column model
-
-		/*! \brief Fill values from row into dialog
-		 *
-		 * This method is used to fill the given values into the edit dialog
-		 *
-		 * \param[in] p_row Row containing the values
-		 */
-		void setupEditDialog(Gtk::TreeModel::Row);
-
-		/*! \brief Get integer index from compression name
-		 *
-		 * This method returns the index of the given compression name
-		 * within the static array of all compression names
-		 *
-		 * \param[in] p_compName Compression name
-		 *
-		 * \return Index within array
-		 */
-		inline int compNameToIndex(const char *);
-		/*! \brief Get integer index from encryption name
-		 *
-		 * This method returns the index of the given encryption name within
-		 * the static array of all encryption names
-		 *
-		 * \param[in] p_encName Encryption name
-		 *
-		 * \return Index within array
-		 */
-		inline int encNameToIndex(const char *);
+		EditDialog *m_editDlg;         //!< Reference to edit dialog class
 
 	protected:
 		/*! \brief Signal handler for "Add" button

src/gui/EditDialog.cxx

+/*
+ * EditDialog.cxx
+ *
+ * Copyright 2013 Andreas Tscharner <andy@vis.ethz.ch>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+
+#include "EditDialog.hxx"
+#include "../include/CompressDecompress.hxx"
+#include "../include/EncryptDecrypt.hxx"
+
+
+inline int EditDialog::compNameToIndex(const char *p_compName)
+{
+	if (strcmp("None", p_compName) == 0) {
+		return 0;
+	};
+
+	for (int iter = lrc::NoneCompression; iter < lrc::lastCompression; iter++) {
+		if (strcmp(lrc::CompressionTypeNames[iter], p_compName) == 0) {
+			return iter;
+		};
+	};
+
+	return 0;
+}
+
+inline int EditDialog::encNameToIndex(const char *p_encName)
+{
+	if (strcmp("None", p_encName) == 0) {
+		return 0;
+	};
+
+	for (int iter = lrc::NoneEncryption; iter < lrc::lastEncryption; iter++) {
+		if (strcmp(lrc::EncryptionTypeNames[iter], p_encName) == 0) {
+			return iter;
+		};
+	};
+
+	return 0;
+}
+
+
+void EditDialog::on_enc_combo_changed(void)
+{
+	Gtk::Entry *edResPassword;
+
+
+	m_refBuilder->get_widget("edPassword", edResPassword);
+//	edResPassword->set_editable(this->encNameToIndex(encTypeName.c_str()) > lrc::NoneEncryption);
+}
+
+
+EditDialog::EditDialog(Glib::RefPtr<Gtk::Builder> p_refBuilder, ResColumnModel *p_resColModel)
+{
+	m_refBuilder = p_refBuilder;
+	m_resColModel = p_resColModel;
+
+	m_refBuilder->get_widget("dlgEdit", m_editDialog);
+
+	p_refBuilder->get_widget("cbxCompression", m_compSelect);
+	for (unsigned int iter = lrc::NoneCompression; iter < lrc::lastCompression; iter++) {
+		m_compSelect->append(lrc::CompressionTypeNames[iter]);
+	};
+
+	p_refBuilder->get_widget("cbxEncryption", m_encSelect);
+	for (unsigned int iter = lrc::NoneEncryption; iter < lrc::lastEncryption; iter++) {
+		m_encSelect->append(lrc::EncryptionTypeNames[iter]);
+	};
+}
+
+EditDialog::~EditDialog(void)
+{
+	delete m_encSelect;
+	delete m_compSelect;
+	delete m_editDialog;
+}
+
+int EditDialog::show_dialog(Gtk::TreeModel::Row p_row)
+{
+	Gtk::Entry *edResID;
+	Gtk::Entry *edResFilename;
+	Gtk::Entry *edResPassword;
+	Glib::ustring compTypeName;
+	Glib::ustring encTypeName;
+	int retVal;
+
+
+	if (!this->m_editDialog)
+		return -1;
+
+	this->m_compSelect->set_active(0);
+	this->m_encSelect->set_active(0);
+
+	this->m_refBuilder->get_widget("edID", edResID);
+	edResID->set_text(p_row[this->m_resColModel->FUniqueResID]);
+
+	this->m_refBuilder->get_widget("edFilename", edResFilename);
+	edResFilename->set_text(p_row[this->m_resColModel->FResFilename]);
+
+	compTypeName = p_row[this->m_resColModel->FCompressTypeName];
+	this->m_compSelect->set_active(this->compNameToIndex(compTypeName.c_str()));
+
+	encTypeName = p_row[this->m_resColModel->FEncryptTypeName];
+	this->m_encSelect->set_active(this->encNameToIndex(encTypeName.c_str()));
+
+	this->m_refBuilder->get_widget("edPassword", edResPassword);
+	if (this->encNameToIndex(encTypeName.c_str()) > lrc::NoneEncryption) {
+		edResPassword->set_text(p_row[this->m_resColModel->FEncPassword]);
+	};
+	edResPassword->set_editable(this->encNameToIndex(encTypeName.c_str()) > lrc::NoneEncryption);
+
+	retVal = this->m_editDialog->run();
+	this->m_editDialog->hide();
+
+	return retVal;
+}

src/gui/EditDialog.hxx

+/*
+ * EditDialog.hxx
+ *
+ * Copyright 2013 Andreas Tscharner <andy@vis.ethz.ch>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA 02110-1301, USA.
+ */
+
+
+/*! \file EditDialog.hxx
+ *
+ * This file contains the EditDialog class. It is responsible for the
+ * logic for the edit dialog of one entry
+ *
+ * \author Andreas Tscharner
+ * \date 2013-07-10
+ */
+
+
+#ifndef __EDITDIALOG_HXX__
+#define __EDITDIALOG_HXX__
+
+
+#include <gtkmm/builder.h>
+#include <gtkmm/dialog.h>
+#include <gtkmm/comboboxtext.h>
+#include "ResColumnModel.hxx"
+
+
+/*! \class EditDialog
+ *  \brief Class to handle the edit dialog for one entry
+ *
+ * Class that handles the logic for the edit dialog which is responsible
+ * for one resource entry
+ */
+class EditDialog
+{
+	private:
+		Glib::RefPtr<Gtk::Builder> m_refBuilder; //!< Reference to main builder
+		Gtk::Dialog *m_editDialog;               //!< Reference to dialog for editing the selected resource
+		Gtk::ComboBoxText *m_compSelect;         //!< Reference to compression combobox
+		Gtk::ComboBoxText *m_encSelect;          //!< Reference to encryption combobox
+		ResColumnModel *m_resColModel;           //!< Reference to instance of column model
+
+		/*! \brief Get integer index from compression name
+		 *
+		 * This method returns the index of the given compression name
+		 * within the static array of all compression names
+		 *
+		 * \param[in] p_compName Compression name
+		 *
+		 * \return Index within array
+		 */
+		inline int compNameToIndex(const char *);
+		/*! \brief Get integer index from encryption name
+		 *
+		 * This method returns the index of the given encryption name within
+		 * the static array of all encryption names
+		 *
+		 * \param[in] p_encName Encryption name
+		 *
+		 * \return Index within array
+		 */
+		inline int encNameToIndex(const char *);
+
+	protected:
+		/*! \brief Signal handler for changed encryption combo box
+		 *
+		 * This method is called whenever the encryption combo box changes its
+		 * value
+		 */
+		void on_enc_combo_changed(void);
+
+	public:
+		/*! \brief Constructor
+		 *
+		 * The constructor sets up the dialog fo editing with the
+		 * given values
+		 *
+		 * \param[in] p_refBuilder Reference to the builder that loaded the
+		 *                         glade GUI file
+		 * \param[in] p_resColModel Reference to instance of column model
+		 */
+		EditDialog(Glib::RefPtr<Gtk::Builder>, ResColumnModel *);
+		/*! \brief Destructor
+		 *
+		 * Frees all the used memory
+		 */
+		~EditDialog(void);
+
+		/*! \brief Shows the dialog with the given data
+		 *
+		 * This method shows the dialog with the given data and hides
+		 * it again after the user clicked [OK] or [ESC]
+		 *
+		 * \param[in] p_row Row containing the values
+		 *
+		 * \return Value of clicked button
+		 */
+		int show_dialog(Gtk::TreeModel::Row);
+};
+
+
+#endif /* __EDITDIALOG_HXX__ */

src/gui/Makefile.am

                             ButtonHandlers.hxx ButtonHandlers.cxx       \
                             ResColumnModel.hxx ResColumnModel.cxx       \
                             ListHandler.hxx ListHandler.cxx             \
+                            EditDialog.hxx EditDialog.cxx               \
                             lrcResourceEditor.cxx
 
 #
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.