Revision: 6176
Author: nogu.dev
Date: Sat Feb 27 15:03:01 2010
Log: * qt4/candwin/qt4.cpp
* qt4/candwin/qt4.h
  - Replace CandidateListView with QTableWidget.
    CandidateListView inherits Q3ListView.
    This commit contains a big change.
    Because this changes the appearance of uim-candwin-qt4,
    you shouldn't merge this into the 1.5 branch.
http://code.google.com/p/uim/source/detail?r=6176

Modified:
 /trunk/qt4/candwin/qt4.cpp
 /trunk/qt4/candwin/qt4.h

=======================================
--- /trunk/qt4/candwin/qt4.cpp  Sat Feb 27 15:02:35 2010
+++ /trunk/qt4/candwin/qt4.cpp  Sat Feb 27 15:03:01 2010
@@ -36,11 +36,12 @@
 #include <QDesktopWidget>
 #include <qlabel.h>
 #include <qwidget.h>
-#include <Q3Header>
 #include <qsocketnotifier.h>
 #include <qstringlist.h>
 #include <qtextcodec.h>
 #include <qrect.h>
+#include <QtGui/QHeaderView>
+#include <QtGui/QTableWidget>
 #include <QtGui/QVBoxLayout>

 #include <clocale>
@@ -75,19 +76,18 @@
     setFocusPolicy( Qt::NoFocus );

     //setup CandidateList
-    cList = new CandidateListView( this, "candidateListView" );
-    cList->setSorting( -1 );
-    cList->setSelectionMode( Q3ListView::Single );
-    cList->addColumn( "1" );
-    cList->setColumnWidthMode( 0, Q3ListView::Maximum );
-    cList->addColumn( "2" );
-    cList->setColumnWidthMode( 1, Q3ListView::Maximum );
-    cList->header() ->hide();
-    cList->setVScrollBarMode( Q3ScrollView::AlwaysOff );
-    cList->setHScrollBarMode( Q3ScrollView::AlwaysOff );
-    cList->setAllColumnsShowFocus( true );
-    connect( cList, SIGNAL( clicked( Q3ListViewItem * ) ),
- this , SLOT( slotCandidateSelected( Q3ListViewItem * ) ) );
+    cList = new QTableWidget;
+    cList->setSelectionMode( QAbstractItemView::SingleSelection );
+    cList->setSelectionBehavior( QAbstractItemView::SelectRows );
+    cList->setColumnCount( 2 );
+ cList->horizontalHeader()->setResizeMode( QHeaderView::ResizeToContents );
+    cList->horizontalHeader()->hide();
+    cList->verticalHeader()->hide();
+    cList->setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
+    cList->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
+    cList->setShowGrid( false );
+    connect( cList, SIGNAL( itemClicked( QTableWidgetItem * ) ),
+          this , SLOT( slotCandidateSelected( QTableWidgetItem * ) ) );

     //setup NumberLabel
     numLabel = new QLabel( this, "candidateLabel" );
@@ -129,7 +129,8 @@
      */

     // remove old data
-    cList->clear();
+    cList->clearContents();
+    cList->setRowCount( 0 );
     stores.clear();

     // get charset and create codec
@@ -267,7 +268,8 @@
         return ;

     // remove old data
-    cList->clear();
+    cList->clearContents();
+    cList->setRowCount( 0 );
     stores.clear();

     // set default value
@@ -413,9 +415,9 @@
     }
 }

-void CandidateWindow::slotCandidateSelected( Q3ListViewItem * item )
-{
- candidateIndex = ( pageIndex * displayLimit ) + cList->itemIndex( item );
+void CandidateWindow::slotCandidateSelected( QTableWidgetItem * item )
+{
+    candidateIndex = ( pageIndex * displayLimit ) + cList->row( item );

     // write message
     fprintf( stdout, "index\n" );
@@ -430,27 +432,19 @@
 #if defined(ENABLE_DEBUG)
     qDebug( "adjustCandidateWindowSize()" );
 #endif
-    int width = 0;
-    int height = 0;
-    Q3ListViewItem *item = cList->firstChild();
-    if ( item )
-        height = item->height() * ( cList->childCount() + 1 );
-
-    // 2004-08-02 Kazuki Ohta <[email protected]>
-    // FIXME!:
- // There may be more proper way. Now width is adjusted by indeterminal 3 spaces.
-    //    Using QWidget::adjustSize() seems not to work properly...
-    int maxCharIndex = 0, maxCharCount = 0;
-    for ( int i = 0; i < cList->childCount(); i++ )
-    {
-        if ( maxCharCount < cList->itemAtIndex( i ) ->text( 1 ).length() )
-        {
-            maxCharIndex = i;
-            maxCharCount = cList->itemAtIndex( i ) ->text( 1 ).length();
-        }
-    }
-    QFontMetrics fm( cList->font() );
- width = fm.width( cList->itemAtIndex( maxCharIndex ) ->text( 0 ) + " " + cList->itemAtIndex( maxCharIndex ) ->text( 1 ) );
+    // frame width
+    int frame = style()->pixelMetric( QStyle::PM_DefaultFrameWidth ) * 2
+        + cList->style()->pixelMetric( QStyle::PM_DefaultFrameWidth ) * 2;
+
+    int rowNum = cList->rowCount();
+    // If cList is empty, the height of cList is 0.
+    int height = ( ( rowNum == 0 ) ? 0 : cList->rowHeight( 0 ) * rowNum )
+        + numLabel->height() + frame;
+
+    int width = frame;
+    for ( int i = 0; i < cList->columnCount(); i++ )
+        width += cList->columnWidth( i );
+
     if ( width < MIN_CAND_WIDTH )
         width = MIN_CAND_WIDTH;

@@ -460,7 +454,8 @@
 void CandidateWindow::setPage( int page )
 {
     // clear items
-    cList->clear();
+    cList->clearContents();
+    cList->setRowCount( 0 );

     // calculate page
     int newpage, lastpage;
@@ -511,13 +506,23 @@
     int ncandidates = displayLimit;
     if ( newpage == lastpage )
         ncandidates = nrCandidates - displayLimit * lastpage;
-    for ( int i = ncandidates - 1; i >=0 ; i-- )
+    for ( int i = 0; i < ncandidates ; i++ )
     {
         QString headString = stores[ displayLimit * newpage + i ].label;
         QString candString = stores[ displayLimit * newpage + i ].str;

         // insert new item to the candidate list
-        new Q3ListViewItem( cList, headString, candString );
+        QTableWidgetItem *headItem = new QTableWidgetItem;
+        headItem->setText( headString );
+        headItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
+        QTableWidgetItem *candItem = new QTableWidgetItem;
+        candItem->setText( candString );
+        candItem->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
+        int count = cList->rowCount();
+        cList->setRowCount( count + 1 );
+        cList->setItem( count, 0, headItem );
+        cList->setItem( count, 1, candItem );
+ cList->setRowHeight( count, QFontMetrics( cList->font() ).height() );
     }

     // set index
@@ -557,8 +562,12 @@
         if ( displayLimit )
             pos = candidateIndex % displayLimit;

- if ( cList->itemAtIndex( pos ) && ! ( cList->itemAtIndex( pos ) ->isSelected() ) )
-            cList->setSelected( cList->itemAtIndex( pos ), true );
+ if ( cList->item( pos, 0 ) && !cList->item( pos, 0 )->isSelected() )
+        {
+            cList->clearSelection();
+            cList->item( pos, 0 )->setSelected( true );
+            cList->item( pos, 1 )->setSelected( true );
+        }
     }
     else
     {
=======================================
--- /trunk/qt4/candwin/qt4.h    Sat Feb 27 15:02:49 2010
+++ /trunk/qt4/candwin/qt4.h    Sat Feb 27 15:03:01 2010
@@ -33,12 +33,13 @@
 #ifndef UIM_QT4_CANDWIN_QT_H
 #define UIM_QT4_CANDWIN_QT_H

-#include <Q3ListView>
 #include <QtCore/QList>
+#include <QtGui/QFrame>

 class QLabel;
-class CandidateListView;
 class QStringList;
+class QTableWidget;
+class QTableWidgetItem;

 struct CandData
 {
@@ -65,7 +66,7 @@

 public slots:
     void slotStdinActivated( int );
-    void slotCandidateSelected( Q3ListViewItem* );
+    void slotCandidateSelected( QTableWidgetItem* );

 protected:
     void strParse( const QString& str );
@@ -77,7 +78,7 @@
     void updateLabel();

 protected:
-    CandidateListView *cList;
+    QTableWidget *cList;
     QLabel *numLabel;

     QList<CandData> stores;
@@ -91,48 +92,4 @@
     bool needHighlight;
 };

-class CandidateListView : public Q3ListView
-{
-    Q_OBJECT
-
-public:
- CandidateListView( QWidget *parent, const char *name = 0, Qt::WFlags f = 0 ) : Q3ListView( parent, name, f ) {}
-    ~CandidateListView() {}
-
-
-    int itemIndex( const Q3ListViewItem *item ) const
-    {
-        if ( !item )
-            return -1;
-
-        if ( item == firstChild() )
-            return 0;
-        else
-        {
-            Q3ListViewItemIterator it( firstChild() );
-            uint j = 0;
-            for ( ; it.current() && it.current() != item; ++it, ++j ) ;
-
-            if ( !it.current() )
-                return -1;
-            return j;
-        }
-    }
-
-    Q3ListViewItem* itemAtIndex( int index )
-    {
-        if ( index < 0 )
-            return 0;
-
-        int j = 0;
- for ( Q3ListViewItemIterator it = firstChild(); it.current(); ++it )
-        {
-            if ( j == index )
-                return it.current();
-            j++;
-        };
-        return 0;
-    }
-};
-
 #endif  /* UIM_QT4_CANDWIN_QT_H */

Reply via email to