1 /*
2 
3 Copyright (c) 2003-2013 uim Project https://github.com/uim/uim
4 
5 All rights reserved.
6 
7 Redistribution and use in source and binary forms, with or without
8 modification, are permitted provided that the following conditions
9 are met:
10 
11 1. Redistributions of source code must retain the above copyright
12 notice, this list of conditions and the following disclaimer.
13 2. Redistributions in binary form must reproduce the above copyright
14 notice, this list of conditions and the following disclaimer in the
15 documentation and/or other materials provided with the distribution.
16 3. Neither the name of authors nor the names of its contributors
17 may be used to endorse or promote products derived from this software
18 without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND
21 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 SUCH DAMAGE.
31 
32 */
33 #include "chargridview.h"
34 
35 #include <QtGui/QFont>
36 #include <QtGui/QMouseEvent>
37 #include <QtGui/QResizeEvent>
38 #if QT_VERSION < 0x050000
39 # include <QtGui/QHeaderView>
40 # include <QtGui/QScrollBar>
41 #else
42 # include <QtWidgets/QHeaderView>
43 # include <QtWidgets/QScrollBar>
44 #endif
45 
46 static const int COLS = 10;
47 
CharGridView(int x,int y,QWidget * parent)48 CharGridView::CharGridView( int x, int y, QWidget *parent )
49         : QTableWidget( parent )
50 {
51     setColumnCount( x );
52     setRowCount( y );
53     QHeaderView *header = horizontalHeader();
54     header->setVisible( false );
55     for ( int i = 0; i < x; i++)
56     {
57 #if QT_VERSION < 0x050000
58         header->setResizeMode( i, QHeaderView::Fixed );
59 #else
60         header->setSectionResizeMode( i, QHeaderView::Fixed );
61 #endif
62         header->resizeSection( i, 30 );
63     }
64     header = verticalHeader();
65     header->setVisible( false );
66     for ( int i = 0; i < y; i++)
67     {
68 #if QT_VERSION < 0x050000
69         header->setResizeMode( i, QHeaderView::Fixed );
70 #else
71         header->setSectionResizeMode( i, QHeaderView::Fixed );
72 #endif
73         header->resizeSection( i, 30 );
74     }
75     setSelectionBehavior( QAbstractItemView::SelectItems );
76     setSelectionMode( QAbstractItemView::SingleSelection );
77 
78     setHorizontalScrollBarPolicy( Qt::ScrollBarAlwaysOff );
79 
80 
81     setFrameStyle( QFrame::NoFrame );
82     show();
83 }
84 
~CharGridView()85 CharGridView::~CharGridView()
86 {}
87 
mousePressEvent(QMouseEvent * e)88 void CharGridView::mousePressEvent( QMouseEvent * e )
89 {
90     if ( e->button() != Qt::LeftButton )
91         return;
92     QTableWidget::mousePressEvent( e );
93 }
94 
mouseReleaseEvent(QMouseEvent * e)95 void CharGridView::mouseReleaseEvent( QMouseEvent * e )
96 {
97     if ( e->button() != Qt::LeftButton )
98         return;
99 
100     QTableWidgetItem *item = itemAt( e->pos() );
101     emit charSelected( item ? item->text() : QString() );
102 }
103 
sizeHint() const104 QSize CharGridView::sizeHint() const
105 {
106     return QSize( columnCount() * horizontalHeader()->sectionSize( 0 ),
107         rowCount() * verticalHeader()->sectionSize( 0 ) );
108 }
109 
setCharFont(const QFont & font)110 void CharGridView::setCharFont( const QFont &font )
111 {
112     setFont( font );
113     for ( int i = 0; i < rowCount(); i++)
114     {
115         for ( int j = 0; j < columnCount(); j++)
116         {
117             QTableWidgetItem *cell = item( i, j );
118             if ( cell )
119                 cell->setFont( font );
120         }
121     }
122 }
123 
resizeEvent(QResizeEvent * e)124 void CharGridView::resizeEvent( QResizeEvent *e )
125 {
126     Q_UNUSED( e )
127     updateCharGridView();
128 }
129 
setCharacters(const QStringList & charList)130 void CharGridView::setCharacters( const QStringList &charList )
131 {
132     int total = charList.count();
133 
134     int cols = COLS;
135     int rows = total / COLS;
136     if ( total % COLS > 0 )
137     {
138         rows++;
139     }
140     int prevCols = columnCount();
141     int prevRows = rowCount();
142     setColumnCount( cols );
143     setRowCount( rows );
144     QHeaderView *header = horizontalHeader();
145     for ( int i = prevCols + 1; i < cols; i++)
146     {
147 #if QT_VERSION < 0x050000
148         header->setResizeMode( i, QHeaderView::Fixed );
149 #else
150         header->setSectionResizeMode( i, QHeaderView::Fixed );
151 #endif
152         header->resizeSection( i, header->sectionSize( 0 ) );
153     }
154     header = verticalHeader();
155     for ( int i = prevRows + 1; i < rows; i++)
156     {
157 #if QT_VERSION < 0x050000
158         header->setResizeMode( i, QHeaderView::Fixed );
159 #else
160         header->setSectionResizeMode( i, QHeaderView::Fixed );
161 #endif
162         header->resizeSection( i, header->sectionSize( 0 ) );
163     }
164     for ( int i = 0; i < rows; i++)
165     {
166         for ( int j = 0; j < cols; j++)
167         {
168             int index = i * COLS + j;
169             QString str = index < total ? charList.at( index ) : QString();
170             QTableWidgetItem *cell = item( i, j );
171             if ( !cell )
172             {
173                 cell = new QTableWidgetItem;
174                 cell->setTextAlignment( Qt::AlignCenter );
175                 cell->setFlags( Qt::ItemIsSelectable | Qt::ItemIsEnabled );
176                 cell->setFont( font() );
177                 setItem( i, j, cell );
178             }
179             cell->setText( str );
180         }
181     }
182 
183     updateCharGridView();
184 }
185 
updateCharGridView()186 void CharGridView::updateCharGridView()
187 {
188     int cellsize = 0;
189     QScrollBar *vScrollBar = verticalScrollBar();
190     /**
191      * 2004-12-06 Kazuki Ohta <mover@hct.zaq.ne.jp>
192      * FIXME:
193      * The timing vScrollBar is shown is tricky.
194      * So this code doesn't work properly.
195      * hmm..
196      */
197     /*
198     if( vScrollBar->isShown() )
199     {
200         qDebug("vScrollBar->isShown() = true");
201         cellsize = (width() - vScrollBar->minimumWidth())/numCols();
202     }
203     else
204     {
205         qDebug("vScrollBar->isShown() = false");
206         cellsize = width()/numCols();
207     }
208     */
209     // adhoc code
210     // but minimumWidth() is always 0 ?
211     cellsize = ( width() - vScrollBar->minimumWidth() ) / columnCount();
212 
213     QHeaderView *header = horizontalHeader();
214     for ( int i = 0; i < columnCount(); i++)
215         header->resizeSection( i, cellsize );
216     header = verticalHeader();
217     for ( int i = 0; i < rowCount(); i++)
218         header->resizeSection( i, cellsize );
219 }
220