Revision: 7288
Author:   nogu.dev
Date:     Sat Sep 10 06:32:36 2011
Log:      * Use dialog instead of embedded widget.
* qt4/pref/customwidgets.cpp
  - (CustomTable::slotEditButtonClicked): New slot.
  - (CustomTable::setTableCustom, CustomTable::slotCellChanged,
     CustomTable::slotAddClicked, CustomTable::slotRemoveClicked): Remove
  - (TableEditForm): New class.
* qt4/pref/customwidgets.h
  - Ditto.
* qt4/pref/qt4.cpp
  - (GroupPageWidget::addCustomTypeTable): Change layout.
http://code.google.com/p/uim/source/detail?r=7288

Modified:
 /trunk/qt4/pref/customwidgets.cpp
 /trunk/qt4/pref/customwidgets.h
 /trunk/qt4/pref/qt4.cpp

=======================================
--- /trunk/qt4/pref/customwidgets.cpp   Fri Sep  9 19:58:06 2011
+++ /trunk/qt4/pref/customwidgets.cpp   Sat Sep 10 06:32:36 2011
@@ -1197,31 +1197,14 @@
     : QFrame( parent ),
       UimCustomItemIface( c )
 {
-    m_table = new QTableWidget;
-    m_table->setSelectionMode( QAbstractItemView::SingleSelection );
-    m_table->horizontalHeader()->setVisible( false );
-    m_table->verticalHeader()->setVisible( false );
-    connect( m_table, SIGNAL(cellChanged(int, int)),
-            this, SLOT(slotCellChanged(int, int)) );
-
-    m_addButton = new QPushButton;
-    m_addButton->setText( _("Add") );
-    connect( m_addButton, SIGNAL(clicked()),
-            this, SLOT(slotAddClicked()) );
-
-    m_removeButton = new QPushButton;
-    m_removeButton->setText( _("Remove") );
-    connect( m_removeButton, SIGNAL(clicked()),
-            this, SLOT(slotRemoveClicked()) );
-
-    QVBoxLayout *buttonLayout = new QVBoxLayout;
-    buttonLayout->addWidget( m_addButton );
-    buttonLayout->addWidget( m_removeButton );
-    buttonLayout->addStretch();
+    m_editButton = new QPushButton;
+    m_editButton->setText( _("Edit") );
+    connect( m_editButton, SIGNAL(clicked()),
+            this, SLOT(slotEditButtonClicked()) );

     QHBoxLayout *layout = new QHBoxLayout;
-    layout->addWidget( m_table );
-    layout->addLayout( buttonLayout );
+    layout->addStretch();
+    layout->addWidget( m_editButton );

     setLayout( layout );

@@ -1232,38 +1215,6 @@
 {
     if( !m_custom || m_custom->type != UCustom_Table )
         return;
-
-    char ***custom_table = m_custom->value->as_table;
-    if ( custom_table ) {
-        // the number may differ from row to row
-        int max_column = -1;
-        int row;
-        for ( row = 0; custom_table[row]; row++ ) {
-            for ( int column = 0; custom_table[row][column]; column++ ) {
-                if ( max_column < column )
-                    max_column = column;
-            }
-        }
-        m_table->setRowCount( row );
-        m_table->setColumnCount( max_column + 1 );
-
-        // don't call slotCellChanged()
-        m_table->setEnabled( false );
-        for ( int row = 0; custom_table[row]; row++ ) {
-            bool expanded = false;
-            for ( int column = 0; column < max_column + 1; column++ ) {
-                if ( !custom_table[row][column] )
-                    expanded = true;
-                QTableWidgetItem *item = new QTableWidgetItem( expanded ?
-                    "" : _FU8( custom_table[row][column] ) );
-                if ( expanded )
-                    item->setFlags( Qt::NoItemFlags );
-                m_table->setItem( row, column, item );
-            }
-        }
-        m_table->setEnabled( true );
-    }
-
     /* sync with Label */
     parentWidget()->setEnabled( m_custom->is_active );
 }
@@ -1300,21 +1251,99 @@
     update();
 }

-void CustomTable::setTableCustom()
-{
-    char ***custom_table = m_custom->value->as_table;
-    for ( int row = 0; custom_table[row]; row++ ) {
+void CustomTable::slotEditButtonClicked()
+{
+    TableEditForm dialog( this );
+    dialog.setCustomTable( m_custom->value->as_table );
+    if ( dialog.exec() == QDialog::Accepted ) {
+        m_custom->value->as_table = dialog.customTable();
+        setCustom( m_custom );
+        update();
+    }
+}
+
+TableEditForm::TableEditForm( QWidget *parent )
+    : QDialog( parent )
+{
+    m_table = new QTableWidget;
+    m_table->setSelectionMode( QAbstractItemView::SingleSelection );
+    m_table->horizontalHeader()->setVisible( false );
+    m_table->verticalHeader()->setVisible( false );
+
+    m_addButton = new QPushButton;
+    m_addButton->setText( _("Add") );
+    connect( m_addButton, SIGNAL(clicked()),
+            this, SLOT(slotAddClicked()) );
+
+    m_removeButton = new QPushButton;
+    m_removeButton->setText( _("Remove") );
+    connect( m_removeButton, SIGNAL(clicked()),
+            this, SLOT(slotRemoveClicked()) );
+
+    QPushButton *m_okButton = new QPushButton;
+    m_okButton->setText( _("OK") );
+    connect( m_okButton, SIGNAL( clicked() ), this, SLOT( accept() ) );
+
+    QPushButton *m_cancelButton = new QPushButton;
+    m_cancelButton->setText( _("Cancel") );
+    connect( m_cancelButton, SIGNAL( clicked() ), this, SLOT( reject() ) );
+
+    QVBoxLayout *buttonLayout = new QVBoxLayout;
+    buttonLayout->addWidget( m_addButton );
+    buttonLayout->addWidget( m_removeButton );
+    buttonLayout->addStretch();
+    buttonLayout->addWidget( m_okButton );
+    buttonLayout->addWidget( m_cancelButton );
+
+    QHBoxLayout *layout = new QHBoxLayout;
+    layout->addWidget( m_table );
+    layout->addLayout( buttonLayout );
+
+    setLayout( layout );
+
+    m_table->horizontalHeader()->adjustSize();
+}
+
+void TableEditForm::setCustomTable( char ***custom_table )
+{
+    if ( !custom_table )
+        return;
+    // the number may differ from row to row
+    int max_column = -1;
+    int row;
+    for ( row = 0; custom_table[row]; row++ ) {
         for ( int column = 0; custom_table[row][column]; column++ ) {
-            free( custom_table[row][column] );
-        }
-        free( custom_table[row] );
-    }
+            if ( max_column < column )
+                max_column = column;
+        }
+    }
+    m_table->setRowCount( row );
+    m_table->setColumnCount( max_column + 1 );
+
+    // don't call slotCellChanged()
+    m_table->setEnabled( false );
+    for ( int row = 0; custom_table[row]; row++ ) {
+        bool expanded = false;
+        for ( int column = 0; column < max_column + 1; column++ ) {
+            if ( !custom_table[row][column] )
+                expanded = true;
+            QTableWidgetItem *item = new QTableWidgetItem( expanded ?
+                "" : _FU8( custom_table[row][column] ) );
+            if ( expanded )
+                item->setFlags( Qt::NoItemFlags );
+            m_table->setItem( row, column, item );
+        }
+    }
+    m_table->setEnabled( true );
+}
+
+char ***TableEditForm::customTable() const
+{
     int rowCount = m_table->rowCount();
-    custom_table = (char ***)malloc( sizeof(char **) * ( rowCount + 1 ) );
+    char ***custom_table
+            = (char ***)malloc( sizeof(char **) * ( rowCount + 1 ) );
     custom_table[rowCount] = 0;

-    m_custom->value->as_table = custom_table;
-
     int columnCount = m_table->columnCount();

     for ( int row = 0; row < rowCount; row++ ) {
@@ -1333,19 +1362,10 @@
                 m_table->item( row, column )->text().toUtf8().data() );
     }

-    setCustom( m_custom );
+    return custom_table;
 }

-void CustomTable::slotCellChanged(int row, int column)
-{
-    if ( !m_table->isEnabled() )
-        return;
-    m_custom->value->as_table[row][column]
-        = strdup( m_table->item( row, column )->text().toUtf8().data() );
-    setCustom( m_custom );
-}
-
-void CustomTable::slotAddClicked()
+void TableEditForm::slotAddClicked()
 {
     QList<QTableWidgetItem *> items = m_table->selectedItems();
int row = ( items.count() > 0 ) ? items[0]->row() + 1 : m_table->rowCount();
@@ -1356,16 +1376,14 @@
         m_table->setItem( row, i, new QTableWidgetItem( "" ) );
     m_table->setEnabled( true );
     m_table->scrollToItem( m_table->item( row, 0 ) );
-    setTableCustom();
 }

-void CustomTable::slotRemoveClicked()
+void TableEditForm::slotRemoveClicked()
 {
     QList<QTableWidgetItem *> items = m_table->selectedItems();
     if ( items.count() != 1 )
         return;
     m_table->removeRow( items[0]->row() );
-    setTableCustom();
 }

 static QString unicodeKeyToSymStr ( QChar c )
=======================================
--- /trunk/qt4/pref/customwidgets.h     Fri Sep  9 19:58:06 2011
+++ /trunk/qt4/pref/customwidgets.h     Sat Sep 10 06:32:36 2011
@@ -307,17 +307,32 @@

     virtual void update();
     virtual void setDefault();
+private slots:
+    void slotEditButtonClicked();
 private:
-    void setTableCustom();
-    QTableWidget *m_table;
-    QPushButton *m_addButton;
-    QPushButton *m_removeButton;
+    QPushButton *m_editButton;
 protected:
     void currentCustomValueChanged(){ emit customValueChanged(); }
 signals:
     void customValueChanged();
+};
+
+
+class TableEditForm : public QDialog
+{
+    Q_OBJECT
+
+public:
+    TableEditForm( QWidget *parent );
+
+    void setCustomTable( char ***custom_table );
+    char ***customTable() const;
+
+private:
+    QTableWidget *m_table;
+    QPushButton *m_addButton;
+    QPushButton *m_removeButton;
 private slots:
-    void slotCellChanged(int row, int column);
     void slotAddClicked();
     void slotRemoveClicked();
 };
=======================================
--- /trunk/qt4/pref/qt4.cpp     Fri Sep  9 19:58:06 2011
+++ /trunk/qt4/pref/qt4.cpp     Sat Sep 10 06:32:36 2011
@@ -705,7 +705,7 @@
     connect( tableBox, SIGNAL(customValueChanged()),
                       this, SLOT(slotCustomValueChanged()) );

-    QVBoxLayout *layout = new QVBoxLayout;
+    QHBoxLayout *layout = new QHBoxLayout;
     layout->setMargin( 0 );
     layout->setSpacing( 6 );
     layout->addWidget( label );

Reply via email to