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 <qpainter.h>
36 #include <qpoint.h>
37 #include <qbrush.h>
38 #include <qdrawutil.h>
39 
40 static const int COLS = 10;
41 
CharGridView(int x,int y,QWidget * parent,const char * name)42 CharGridView::CharGridView( int x, int y, QWidget *parent, const char *name )
43         : QGridView( parent, name )
44 {
45     setNumCols( x );
46     setNumRows( y );
47     setCellWidth( 30 );
48     setCellHeight( 30 );
49 
50     setHScrollBarMode( QScrollView::AlwaysOff );
51 
52     m_activeCell.setX( -1 );
53     m_activeCell.setY( -1 );
54 
55     m_font = font();
56 
57     setFrameStyle( QFrame::NoFrame );
58     show();
59 }
~CharGridView()60 CharGridView::~CharGridView()
61 {}
paintCell(QPainter * painter,int y,int x)62 void CharGridView::paintCell( QPainter * painter, int y, int x )
63 {
64     // set font
65     if ( m_font != font() )
66         painter->setFont( m_font );
67 
68     bool isActiveCell = ( ( m_activeCell.x() == x ) && ( m_activeCell.y() == y ) );
69     if ( isActiveCell )
70     {
71         // save the painter's state to the stack and swap back/fore ground colors
72         painter->save();
73         QColor tmp( painter->pen().color() );
74         painter->setPen( painter->backgroundColor() );
75         painter->setBackgroundColor( tmp );
76     }
77 
78     QBrush bBrush( colorGroup().base() );
79     QBrush tBrush( colorGroup().text() );
80     if ( isActiveCell )
81         qDrawShadePanel( painter, 0, 0, cellWidth(), cellHeight(), colorGroup(), false, 2, &tBrush ); //&(painter->brush()));
82     else
83         qDrawPlainRect( painter, 0, 0, cellWidth(), cellHeight(), colorGroup().foreground(), 1, &bBrush ); //&(painter->brush()));
84 
85     QString c = coordsToChar( x, y );
86     if ( !c.isEmpty() )
87         painter->drawText( 0, 0, cellWidth(), cellHeight(), AlignCenter, c );
88 
89     // restore state
90     if ( isActiveCell )
91         painter->restore();
92 }
93 
contentsMousePressEvent(QMouseEvent * e)94 void CharGridView::contentsMousePressEvent( QMouseEvent * e )
95 {
96     if ( e->button() != LeftButton )
97         return ;
98 
99     int y = e->pos().y();
100     int x = e->pos().x();
101 
102     int row = rowAt( y );
103     int col = columnAt( x );
104 
105     if ( ( m_activeCell.y() != row ) || ( m_activeCell.x() != col ) )
106     {
107         int oldactivecelly = m_activeCell.y();
108         int oldactivecellx = m_activeCell.x();
109         m_activeCell.setY( row );
110         m_activeCell.setX( col );
111         updateCell( oldactivecelly, oldactivecellx );
112         updateCell( m_activeCell.y(), m_activeCell.x() );
113     }
114 }
115 
contentsMouseReleaseEvent(QMouseEvent * e)116 void CharGridView::contentsMouseReleaseEvent( QMouseEvent * e )
117 {
118     if ( e->button() != LeftButton )
119         return ;
120 
121     emit charSelected( coordsToChar( m_activeCell.x(), m_activeCell.y() ) );
122 }
coordsToChar(int x,int y)123 QString CharGridView::coordsToChar( int x, int y )
124 {
125     if ( x < 0 || x > numCols() || y < 0 || y > numRows() )
126         debug( "coordsToIndex: invalid coords(%d, %d)\n", x, y );
127 
128     unsigned int index = y * numCols() + x;
129     if ( index < m_charList.count() )
130         return m_charList[ index ];
131     else
132         return QString::null;
133 }
134 
sizeHint(void) const135 QSize CharGridView::sizeHint( void ) const
136 {
137     return QSize( numCols() * cellWidth(), numRows() * cellHeight() );
138 }
139 
resizeEvent(QResizeEvent *)140 void CharGridView::resizeEvent( QResizeEvent * /* e */ )
141 {
142     updateCharGridView();
143 }
144 
setCharacters(const QStringList & charList)145 void CharGridView::setCharacters( const QStringList &charList )
146 {
147     // default position
148     m_activeCell.setX( -1 );
149     m_activeCell.setY( -1 );
150 
151     m_charList.clear();
152     m_charList = charList;
153 
154     int total = m_charList.count();
155 
156     int cols = COLS;
157     int rows = total / COLS;
158     if ( total % COLS > 0 )
159     {
160         rows++;
161     }
162 
163     setNumCols( cols );
164     setNumRows( rows );
165 
166     updateCharGridView();
167 
168     // repaint all cells
169     for ( int i = 0; i < numRows(); i++ )
170         for ( int j = 0; j < numCols(); j++ )
171             repaintCell( i, j, true );
172 }
updateCharGridView()173 void CharGridView::updateCharGridView()
174 {
175     int cellsize = 0;
176     QScrollBar *vScrollBar = verticalScrollBar();
177     /**
178      * 2004-12-06 Kazuki Ohta <mover@hct.zaq.ne.jp>
179      * FIXME:
180      * The timing vScrollBar is shown is tricky.
181      * So this code doesn't work properly.
182      * hmm..
183      */
184     /*
185     if( vScrollBar->isShown() )
186     {
187         qDebug("vScrollBar->isShown() = true");
188         cellsize = (width() - vScrollBar->minimumWidth())/numCols();
189     }
190     else
191     {
192         qDebug("vScrollBar->isShown() = false");
193         cellsize = width()/numCols();
194     }
195     */
196     // adhoc code
197     // but minimumWidth() is always 0 ?
198     cellsize = ( width() - vScrollBar->minimumWidth() ) / numCols();
199 
200     setCellWidth( cellsize );
201     setCellHeight( cellsize );
202 }
203 
204 #include "chargridview.moc"
205