XML Mill  1.0.0
A GUI based XML editor with a memory.
gcadditemsform.cpp
00001 /* Copyright (c) 2012 - 2013 by William Hallatt.
00002  *
00003  * This file forms part of "XML Mill".
00004  *
00005  * The official website for this project is <http://www.goblincoding.com> and,
00006  * although not compulsory, it would be appreciated if all works of whatever
00007  * nature using this source code (in whole or in part) include a reference to
00008  * this site.
00009  *
00010  * Should you wish to contact me for whatever reason, please do so via:
00011  *
00012  *                 <http://www.goblincoding.com/contact>
00013  *
00014  * This program is free software: you can redistribute it and/or modify it under
00015  * the terms of the GNU General Public License as published by the Free Software
00016  * Foundation, either version 3 of the License, or (at your option) any later
00017  * version.
00018  *
00019  * This program is distributed in the hope that it will be useful, but WITHOUT
00020  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00021  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
00022  *
00023  * You should have received a copy of the GNU General Public License along with
00024  * this program (GNUGPL.txt).  If not, see
00025  *
00026  *                    <http://www.gnu.org/licenses/>
00027  */
00028 
00029 #include "gcadditemsform.h"
00030 #include "ui_gcadditemsform.h"
00031 #include "db/gcdatabaseinterface.h"
00032 #include "utils/gcmessagespace.h"
00033 #include "utils/gcglobalspace.h"
00034 #include "utils/gctreewidgetitem.h"
00035 
00036 #include <QMessageBox>
00037 
00038 /*--------------------------------------------------------------------------------------*/
00039 
00040 const QString CREATE_NEW = "Create New Element";
00041 
00042 /*--------------------------------------------------------------------------------------*/
00043 
00044 GCAddItemsForm::GCAddItemsForm( QWidget* parent )
00045 : QDialog( parent ),
00046   ui     ( new Ui::GCAddItemsForm )
00047 {
00048   ui->setupUi( this );
00049   ui->showHelpButton->setVisible( GCGlobalSpace::showHelpButtons() );
00050 
00051   connect( ui->addNewButton, SIGNAL( clicked() ), this, SLOT( addElementAndAttributes() ) );
00052   connect( ui->donePushButton, SIGNAL( clicked() ), this, SLOT( close() ) );
00053   connect( ui->showHelpButton, SIGNAL( clicked() ), this, SLOT( showHelp() ) );
00054   connect( ui->comboBox, SIGNAL( activated( const QString& ) ), this, SLOT( comboValueChanged( const QString& ) ) );
00055 
00056   populateCombo();
00057   ui->treeWidget->populateFromDatabase();
00058 
00059   if( ui->treeWidget->topLevelItemCount() != 0 )
00060   {
00061     ui->treeWidget->setCurrentItem( ui->treeWidget->topLevelItem( 0 ) );
00062   }
00063 
00064   ui->lineEdit->setFocus();
00065   setAttribute( Qt::WA_DeleteOnClose );
00066 }
00067 
00068 /*--------------------------------------------------------------------------------------*/
00069 
00070 GCAddItemsForm::~GCAddItemsForm()
00071 {
00072   delete ui;
00073 }
00074 
00075 /*--------------------------------------------------------------------------------------*/
00076 
00077 void GCAddItemsForm::populateCombo()
00078 {
00079   ui->comboBox->clear();
00080 
00081   /* It should not be possible to add the root element as a child to any other element. */
00082   QStringList elements( GCDataBaseInterface::instance()->knownElements() );
00083 
00084   foreach( QString root, GCDataBaseInterface::instance()->knownRootElements() )
00085   {
00086    elements.removeAll( root );
00087   }
00088 
00089   ui->comboBox->addItem( CREATE_NEW );
00090   ui->comboBox->addItems( elements );
00091   comboValueChanged( CREATE_NEW );
00092 }
00093 
00094 /*--------------------------------------------------------------------------------------*/
00095 
00096 void GCAddItemsForm::addElementAndAttributes()
00097 {
00098   QString element = ui->lineEdit->text();
00099 
00100   if( !element.isEmpty() )
00101   {
00102     QStringList attributes = ui->plainTextEdit->toPlainText().split( "\n" );
00103 
00104     if( GCDataBaseInterface::instance()->knownElements().contains( element ) )
00105     {
00106       if( !GCDataBaseInterface::instance()->updateElementAttributes( element, attributes ) )
00107       {
00108         GCMessageSpace::showErrorMessageBox( this, GCDataBaseInterface::instance()->lastError() );
00109       }
00110     }
00111     else
00112     {
00113       if( !GCDataBaseInterface::instance()->addElement( element, QStringList(), attributes ) )
00114       {
00115         GCMessageSpace::showErrorMessageBox( this, GCDataBaseInterface::instance()->lastError() );
00116       }
00117     }
00118 
00119     /* If the profile is empty, add the new element as a root element by default. */
00120     if( GCDataBaseInterface::instance()->isProfileEmpty() )
00121     {
00122       if( !GCDataBaseInterface::instance()->addRootElement( element ) )
00123       {
00124         GCMessageSpace::showErrorMessageBox( this, GCDataBaseInterface::instance()->lastError() );
00125       }
00126 
00127       ui->treeWidget->addItem( element );
00128     }
00129     else
00130     {
00131       /* If the profile isn't empty, the user must specify a parent element. */
00132       if( ui->treeWidget->currentItem() )
00133       {
00134         /* Also add it to the parent element's child list. */
00135         if( !GCDataBaseInterface::instance()->updateElementChildren( ui->treeWidget->gcCurrentItem()->name(),
00136                                                                      QStringList( element ) ) )
00137         {
00138           GCMessageSpace::showErrorMessageBox( this, GCDataBaseInterface::instance()->lastError() );
00139         }
00140 
00141         ui->treeWidget->insertItem( element, 0 );
00142       }
00143       else
00144       {
00145         GCMessageSpace::showErrorMessageBox( this, "Please select a parent element in the tree." );
00146         return;
00147       }
00148     }
00149   }
00150 
00151   populateCombo();
00152   ui->lineEdit->clear();
00153   ui->plainTextEdit->clear();
00154   ui->treeWidget->expandAll();
00155 }
00156 
00157 /*--------------------------------------------------------------------------------------*/
00158 
00159 void GCAddItemsForm::comboValueChanged( const QString& element )
00160 {
00161   if( element != CREATE_NEW )
00162   {
00163     ui->lineEdit->setText( element );
00164     ui->lineEdit->setEnabled( false );
00165 
00166     QStringList attributes = GCDataBaseInterface::instance()->attributes( element );
00167 
00168     ui->plainTextEdit->clear();
00169 
00170     foreach( QString value, attributes )
00171     {
00172       ui->plainTextEdit->insertPlainText( QString( "%1\n" ).arg( value ) );
00173     }
00174   }
00175   else
00176   {
00177     ui->lineEdit->setEnabled( true );
00178     ui->lineEdit->clear();
00179     ui->plainTextEdit->clear();
00180   }
00181 }
00182 
00183 /*--------------------------------------------------------------------------------------*/
00184 
00185 void GCAddItemsForm::showHelp()
00186 {
00187   QMessageBox::information( this,
00188                             "How this works...",
00189                             "The new (or existing) element will be added as a a child of the element highlighted "
00190                             "in the \"Element Hierarchy\" tree (if the active profile is empty, the new element "
00191                             "will become the root/main document element for the current profile).\n\n"
00192                             "Although you can only add one element at a time, you can add all the "
00193                             "element's attributes in one go: simply stick each of them on a separate "
00194                             "line in the text edit area and hit \"Add\" when you're done.\n\n"
00195                             "(If the element doesn't have associated attributes, just "
00196                             "leave the text edit area empty)" );
00197 }
00198 
00199 /*--------------------------------------------------------------------------------------*/