Back to index

im-sdk  12.3.91
lookup.cpp
Go to the documentation of this file.
00001 /*
00002   lookup.cpp
00003   Copyright (C) 2003 Free Standards Group
00004 
00005   Permission is hereby granted, free of charge, to any person obtaining a
00006   copy of this software and associated documentation files (the
00007   "Software"), to deal in the Software without restriction, including
00008   without limitation the rights to use, copy, modify, merge, publish,
00009   distribute, sublicense, and/or sell copies of the Software, and to
00010   permit persons to whom the Software is furnished to do so, subject to
00011   the following conditions: The above copyright notice and this
00012   permission notice shall be included in all copies or substantial
00013   portions of the Software.
00014 
00015   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00016   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00017   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00018   IN NO EVENT SHALL OPENI18N WG OR FREE STANDARDS GROUP. BE LIABLE
00019   FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
00020   CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH
00021   THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE EVEN IF
00022   ADVISED IN ADVANCE OF THE POSSIBILITY OF SUCH DAMAGES.
00023 
00024   Except as contained in this notice, the names of OpenI18N WG and/or
00025   Free Standards Group shall not be used in advertising or otherwise to
00026   promote the sale, use or other dealings in this Software without prior
00027   written authorization from OpenI18N WG and/or Free Standards Group
00028   as applicable.
00029 
00030   Author: ILGYOUNG PARK <Karl.Park@Sun.COM>
00031          TuBo <Gavin.Tu@Sun.COM>
00032 
00033 */
00034 #include <qapplication.h>
00035 
00036 #include "lookup.h"
00037 #include "debug.h"
00038 
00039 static const int MIN_CAND_WIDTH = 80;
00040 
00041 const Qt::WFlags candidateFlag = ( Qt::WType_TopLevel
00042                                    | Qt::WStyle_Customize
00043                                    | Qt::WStyle_StaysOnTop
00044                                    | Qt::WStyle_NoBorder
00045                                    | Qt::WStyle_Tool
00046                                | Qt::WPaintClever       
00047 #if defined(Q_WS_X11)
00048                                    | Qt::WX11BypassWM
00049 #endif
00050 
00051                                    );
00052 
00053 IIIMQCFLookup::IIIMQCFLookup(QWidget *w, IIIMInputContext *ic)
00054        
00055 {
00056   iiimqc = ic;
00057   vbox = new QVBox(w, 0, candidateFlag); 
00058 
00059   numLabel = new QLabel(vbox, "candidateLabel");
00060   lw = new QListBox (vbox, "candidateListBox");
00061   lw->setVScrollBarMode(QScrollView::AlwaysOff);
00062   lw->setHScrollBarMode(QScrollView::AlwaysOff);
00063   lw->setFocusPolicy (QWidget::NoFocus);
00064   
00065 }
00066 
00067 
00068 
00069 IIIMQCFLookup::~IIIMQCFLookup()
00070 {
00071   if(vbox)
00072         delete vbox;
00073 }
00074 
00075 
00076 
00077 void
00078 IIIMQCFLookup::showLookupWindow (QWidget *w)
00079 {
00080   if (lw->count() > 0)
00081   {
00082      vbox->raise();
00083      vbox->show();
00084    }
00085   DEBUG_DO(qDebug("**IIIMQCF: showLookupWindow() after setFocus\n"));
00086 }
00087 
00088 
00089 
00090 void
00091 IIIMQCFLookup::hideLookupWindow (void)
00092 {
00093  vbox->hide();
00094 }
00095 
00096 
00097 
00098 void
00099 IIIMQCFLookup::setLookupChoices (QStringList &lc,
00100                              int size,
00101                              int first_index,
00102                              int last_index,
00103                              int current_index)
00104 
00105 {
00106   clearLookupChoices ();
00107   lw->insertStringList (lc);
00108   lw->setCurrentItem (current_index);
00109   adjustLookupWindowSize();
00110 }
00111 
00112 
00113 void 
00114 IIIMQCFLookup::setLookupTitle(QString title)
00115 {
00116   updateLabel(title);
00117 }
00118 
00119 
00120 void
00121 IIIMQCFLookup::clearLookupChoices (void)
00122 {
00123   lw->clear ();
00124 }
00125 
00126 
00127 bool
00128 IIIMQCFLookup::isVisible () const
00129 {
00130   return lw->isVisible ();
00131 }
00132 
00133 
00134 void 
00135 IIIMQCFLookup::layoutWindow(int x, int y, int w, int h)
00136 {
00137     DEBUG_DO(qDebug("**IIIMQCF: layoutWindow: x is %d, y is %d, w is %d, h is %d\n", x, y, w, h));
00138     QRect focusRect  = QRect(QPoint(x, y + h), vbox->frameSize());
00139 
00140     QRect screenRect = QRect(0, 0,
00141                              QApplication::desktop()->screenGeometry().width(),
00142                              QApplication::desktop()->screenGeometry().height());
00143 
00144     QPoint p = forceInside(screenRect, focusRect);
00145     vbox->move(p);
00146 
00147 }
00148 
00149 
00150 QPoint IIIMQCFLookup::forceInside(const QRect &enclosure, const QRect &prisoner)
00151 {
00152     int new_x, new_y;
00153 
00154     new_x = QMIN(enclosure.right(), prisoner.right()) - prisoner.width() + 1;
00155     new_x = QMAX(enclosure.left(), new_x);
00156     new_y = QMIN(enclosure.bottom(), prisoner.bottom()) - prisoner.height() + 1;
00157     new_y = QMAX(enclosure.top(), new_y);
00158 
00159     return QPoint(new_x, new_y);
00160 }
00161 
00162 
00163 void IIIMQCFLookup::updateLabel(QString title)
00164 {
00165     numLabel->setText(title);
00166 }
00167 
00168 
00169 
00170 void IIIMQCFLookup::adjustLookupWindowSize()
00171 {
00172     DEBUG_DO(qDebug("**IIIMQCF: adjustCandidateWindowSize()"));
00173 
00174     int width  = 0;
00175     int height = 0;
00176     QListBoxItem *item =lw->item(0);
00177     if (item)
00178         height = item->height(lw) * (lw->count() + 1);
00179 
00180     unsigned int maxCharIndex = 0, maxCharCount = 0;
00181     for (int i = 0; i < lw->count(); i++) {
00182         if (maxCharCount < lw->item(i)->text().length()) {
00183             maxCharIndex = i;
00184            maxCharCount = lw->item(i)->text().length();
00185         }
00186     }
00187 
00188     QFontMetrics fm(lw->font());
00189     if(lw->item(maxCharIndex))
00190            width  = fm.width(lw->item(maxCharIndex)->text() + "   " + lw->item(maxCharIndex)->text());
00191 
00192     if (width < MIN_CAND_WIDTH)
00193         width = MIN_CAND_WIDTH;
00194 
00195     vbox->resize(width, height);
00196 }
00197