Back to index

scribus-ng  1.3.4.dfsg+svn20071115
unicodesearch.cpp
Go to the documentation of this file.
00001 /*
00002 For general Scribus (>=1.3.2) copyright and licensing information please refer
00003 to the COPYING file provided with the program. Following this notice may exist
00004 a copyright and/or license notice that predates the release of Scribus 1.3.2
00005 for which a new license (GPL+exception) is in place.
00006 */
00007 #include <qfile.h>
00008 #include <qlistview.h>
00009 #include <qlineedit.h>
00010 #include <qlayout.h>
00011 #include <qcursor.h>
00012 
00013 #include "unicodesearch.h"
00014 #include "unicodesearch.moc"
00015 #include "scpaths.h"
00016 // #include "fonts/scfontmetrics.h"
00017 #include "charzoom.h"
00018 
00019 
00020 extern QPixmap loadIcon(QString nam);
00021 
00022 
00023 UnicodeChooseButton::UnicodeChooseButton(QWidget * parent, const char * name)
00024        : QPushButton(parent, name),
00025        m_searchDialog(0)
00026 {
00027        languageChange();
00028        setToggleButton(true);
00029 
00030        m_searchDialog = new UnicodeSearch(this, "m_searchDialog", false);
00031        Q_CHECK_PTR(m_searchDialog);
00032 
00033        connect(this, SIGNAL(toggled(bool)), this, SLOT(self_toggled(bool)));
00034        connect(m_searchDialog, SIGNAL(setVisibleState(bool)), this, SLOT(setOn(bool)));
00035        //
00036        // listview user inputs
00037        connect(m_searchDialog->unicodeList, SIGNAL(doubleClicked(QListViewItem *, const QPoint &, int)), this, SLOT(unicodeList_chosen(QListViewItem *)));
00038        connect(m_searchDialog->unicodeList, SIGNAL(returnPressed(QListViewItem *)), this, SLOT(unicodeList_chosen(QListViewItem *)));
00039        connect(m_searchDialog->unicodeList, SIGNAL(spacePressed(QListViewItem *)), this, SLOT(unicodeList_chosen(QListViewItem *)));
00040 }
00041 
00042 void UnicodeChooseButton::languageChange()
00043 {
00044        setText(tr("&Search"));
00045 }
00046 
00047 void UnicodeChooseButton::unicodeList_chosen(QListViewItem *item)
00048 {
00049        emit chosenUnicode(item->text(0));
00050        emit toggled(false);
00051 }
00052 
00053 void UnicodeChooseButton::self_toggled(bool state)
00054 {
00055        if (state)
00056        {
00057               m_searchDialog->move(mapToGlobal(rect().bottomLeft()));
00058               m_searchDialog->show();
00059               m_searchDialog->checkForUpdate();
00060        }
00061        else
00062               m_searchDialog->hide();
00063 }
00064 
00065 UnicodeSearch::UnicodeSearch( QWidget* parent, const char* name, bool modal)
00066        : UnicodeSearchBase( parent, name, modal), // WStyle_Customize | WStyle_NoBorder),
00067        m_zoom(0)
00068 {
00069        if (!name)
00070               setName("UnicodeSearch");
00071 
00072        connect(searchEdit, SIGNAL(returnPressed()), this, SLOT(searchEdit_returnPressed()));
00073        connect(unicodeList, SIGNAL(mouseButtonPressed(int, QListViewItem*, const QPoint&, int)), this, SLOT(unicodeList_mouseButtonPressed(int, QListViewItem*, const QPoint&, int)));
00074 }
00075 
00076 void UnicodeSearch::checkForUpdate()
00077 {
00078        if (m_unicodeMap.count()==0)
00079        {
00080               qApp->setOverrideCursor(QCursor(Qt::WaitCursor));
00081               readUnicodeMap();
00082               query();
00083               qApp->restoreOverrideCursor();
00084        }
00085 }
00086 
00087 void UnicodeSearch::readUnicodeMap()
00088 {
00089        m_unicodeMap.clear();
00090 
00091        QFile file(ScPaths::instance().shareDir() + "unicodenameslist.txt");
00092        if (file.open( IO_ReadOnly ) )
00093        {
00094               QStringList list(QStringList::split('\n', file.readAll()));
00095               file.close();
00096 
00097               QStringList line;
00098               for ( QStringList::Iterator it = list.begin(); it != list.end(); ++it )
00099               {
00100                      line = QStringList::split(':', *it);
00101                      m_unicodeMap[line[0]] = line[1].lower();
00102               }
00103        }
00104        else
00105               qDebug("UnicodeSearch: error reading unicodes!");
00106 }
00107 
00108 void UnicodeSearch::query()
00109 {
00110        unicodeList->clear();
00111        QMap<QString,QString>::Iterator it;
00112        for (it = m_unicodeMap.begin(); it != m_unicodeMap.end(); ++it)
00113        {
00114               QListViewItem *item = new QListViewItem(unicodeList, it.key(), it.data());
00115               unicodeList->insertItem(item);
00116        }
00117 }
00118 
00119 void UnicodeSearch::query(QString filter)
00120 {
00121        if (filter.isNull())
00122               return;
00123 
00124        unicodeList->clear();
00125 
00126        QMap<QString,QString>::Iterator it;
00127        for (it = m_unicodeMap.begin(); it != m_unicodeMap.end(); ++it)
00128        {
00129               if (!it.key().contains(filter, false) && !it.data().contains(filter, false))
00130                      continue;
00131               QListViewItem *item = new QListViewItem(unicodeList, it.key(), it.data());
00132               unicodeList->insertItem(item);
00133        }
00134 }
00135 
00136 void UnicodeSearch::searchEdit_returnPressed()
00137 {
00138        if (searchEdit->text().length() == 0)
00139               query();
00140        query(searchEdit->text());
00141 }
00142 
00143 void UnicodeSearch::hideEvent(QHideEvent * e)
00144 {
00145        QDialog::hideEvent(e);
00146        emit setVisibleState(false);
00147 }
00148 
00149 void UnicodeSearch::unicodeList_mouseButtonPressed(int button, QListViewItem* item, const QPoint& point, int)
00150 {
00151        if (!item)
00152               return;
00153        // It must go 1st to delete the existing dialog on click
00154        if (m_zoom)
00155        {
00156               delete m_zoom;
00157               m_zoom = 0;
00158        }
00159        if (button == RightButton && !m_zoom)
00160        {
00161               bool ok;
00162               int val = item->text(0).toInt(&ok, 16);
00163               if (!ok)
00164                      return;
00165               m_zoom = new CharZoom(this, val, m_font);
00166               m_zoom->move(point.x()-2, point.y()-2);
00167               m_zoom->show();
00168        }
00169 }
00170