]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSymbols.cpp
* GuiSymbols.cpp:
[lyx.git] / src / frontends / qt4 / GuiSymbols.cpp
index 96f8f1f9c81f5008bb9dc1cae3aac3408c2e58d6..ab745e8473e52d7965f6584b18927cd8563845bb 100644 (file)
 
 #include "GuiSymbols.h"
 
-#include "Buffer.h"
-#include "BufferView.h"
-
 #include "GuiApplication.h"
 #include "GuiView.h"
-
 #include "qt_helpers.h"
 
+#include "Buffer.h"
+#include "BufferParams.h"
+#include "BufferView.h"
 #include "Encoding.h"
+
 #include "support/gettext.h"
 
+#include <QChar>
 #include <QPixmap>
 #include <QListWidgetItem>
-
-// Set to zero if unicode symbols are preferred.
-#define USE_PIXMAP 1
+#include <QString>
 
 using namespace std;
 
@@ -73,6 +72,7 @@ UnicodeBlocks unicode_blocks[] = {
        { N_("Tibetan"), 0x0f00, 0x0fbf },
        { N_("Georgian"), 0x10a0, 0x10ff },
        { N_("Hangul Jamo"), 0x1100, 0x11ff },
+       { N_("Phonetic Extensions"), 0x1d00, 0x1d7f },
        { N_("Latin Extended Additional"), 0x1e00, 0x1eff },
        { N_("Greek Extended"), 0x1f00, 0x1fff },
        { N_("General Punctuation"), 0x2000, 0x206f },
@@ -144,6 +144,16 @@ UnicodeBlocks unicode_blocks[] = {
 
 const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
 
+
+QString getCodePoint(char_type c)
+{
+       QString cp = QString::number(c, 16);
+       while (cp.size() < 4)
+               cp.prepend('0');
+       cp.prepend("0x");
+       return cp;
+}
+
 } // namespace anon
 
 
@@ -161,18 +171,35 @@ GuiSymbols::GuiSymbols(GuiView & lv)
        int size = font.pointSize() + 3;
        font.setPointSize(size);
        symbolsLW->setFont(font);
-
-       okPB->setEnabled(!chosenLE->text().isEmpty() &&
-               !bufferview()->buffer().isReadonly());
-       applyPB->setEnabled(!chosenLE->text().isEmpty() &&
-               !bufferview()->buffer().isReadonly());
 }
 
 
 void GuiSymbols::updateView()
 {
        chosenLE->clear();
-       initialiseParams(bufferview()->cursor().getEncoding()->name());
+
+       string new_encoding = bufferview()->cursor().getEncoding()->name();
+       if (buffer().params().inputenc != "auto" &&
+           buffer().params().inputenc != "default")
+               new_encoding = buffer().params().encoding().name();
+       if (new_encoding == encoding_)
+               // everything up to date
+               return;
+       if (!new_encoding.empty())
+               encoding_ = new_encoding;
+       bool const utf8 = toqstr(encoding_).startsWith("utf8");
+       if (utf8)
+               categoryFilterCB->setChecked(false);
+       categoryFilterCB->setEnabled(!utf8);
+       updateSymbolList();
+}
+
+
+void GuiSymbols::enableView(bool enable)
+{
+       chosenLE->setEnabled(enable);
+       okPB->setEnabled(enable);
+       applyPB->setEnabled(enable);
 }
 
 
@@ -203,8 +230,9 @@ void GuiSymbols::on_symbolsLW_itemActivated(QListWidgetItem *)
 
 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
 {
-       okPB->setEnabled(!text.isEmpty() && !bufferview()->buffer().isReadonly());
-       applyPB->setEnabled(!text.isEmpty() && !bufferview()->buffer().isReadonly());
+       bool const empty_sel = text.isEmpty();
+       okPB->setEnabled(!empty_sel);
+       applyPB->setEnabled(!empty_sel);
 }
 
 
@@ -219,48 +247,111 @@ void GuiSymbols::on_symbolsLW_itemClicked(QListWidgetItem * item)
        QString const text = item->text();
        if (text.isEmpty())
                return;
-       chosenLE->insert(text);
-       QString const category = getBlock(text.data()->unicode());
-       categoryCO->setCurrentIndex(categoryCO->findText(category));
+       if (chosenLE->isEnabled())
+               chosenLE->insert(text);
+       if (categoryFilterCB->isChecked()) {
+               QString const category = getBlock(text.data()->unicode());
+               categoryCO->setCurrentIndex(categoryCO->findText(category));
+       }
 }
 
 
 void GuiSymbols::on_categoryCO_activated(QString const & text)
 {
-       if (used_blocks.find(text) != used_blocks.end())
-               symbolsLW->scrollToItem(used_blocks[text]);
+       if (!categoryFilterCB->isChecked())
+               updateSymbolList(false);
+       else if (used_blocks.find(text) != used_blocks.end())
+               symbolsLW->scrollToItem(used_blocks[text],
+                       QAbstractItemView::PositionAtTop);
+}
+
+
+void GuiSymbols::on_categoryFilterCB_toggled(bool on)
+{
+       updateSymbolList(on);
+       if (on) {
+               QString const category = categoryCO->currentText();
+               if (used_blocks.find(category) != used_blocks.end())
+                       symbolsLW->scrollToItem(used_blocks[category],
+                               QAbstractItemView::PositionAtTop);
+       }
 }
 
 
-void GuiSymbols::updateSymbolList()
+void GuiSymbols::updateSymbolList(bool update_combo)
 {
+       QString category = categoryCO->currentText();
+       bool const nocategory = category.isEmpty();
+       char_type range_start = 0x0000;
+       char_type range_end = 0x110000;
        symbolsLW->clear();
-       used_blocks.clear();
-       categoryCO->clear();
+       if (update_combo) {
+               used_blocks.clear();
+               categoryCO->clear();
+       }
+       bool const show_all = categoryFilterCB->isChecked();
+
+       if (symbols_.empty() || update_combo) {
+               Encoding enc = *(encodings.getFromLyXName(encoding_));
+               symbols_ = enc.getSymbolsList();
+       }
 
-       typedef set<char_type> SymbolsList;
-       Encoding enc = *(encodings.getFromLyXName(encoding_));
-       SymbolsList symbols = enc.getSymbolsList();
+       if (!show_all) {
+               for (int i = 0 ; i < no_blocks; ++i)
+                       if (unicode_blocks[i].name == fromqstr(category)) {
+                               range_start = unicode_blocks[i].start;
+                               range_end = unicode_blocks[i].end;
+                               break;
+                       }
+       }
 
-       SymbolsList::const_iterator const end = symbols.end();
-       for (SymbolsList::const_iterator it = symbols.begin(); it != end; ++it) {
+       SymbolsList::const_iterator const end = symbols_.end();
+       for (SymbolsList::const_iterator it = symbols_.begin(); it != end; ++it) {
                char_type c = *it;
+               if (!update_combo && !show_all && (c <= range_start || c >= range_end))
+                       continue;
+#if QT_VERSION >= 0x040300
+               QChar::Category const cat = QChar::category(uint(c));
+#else
+               QChar const qc = uint(c);
+               QChar::Category const cat = qc.category();
+#endif
                // we do not want control or space characters
-               if (QChar(c).category() == QChar::Other_Control ||
-                   QChar(c).category() == QChar::Separator_Space)
+               if (cat == QChar::Other_Control || cat == QChar::Separator_Space)
                        continue;
-               QListWidgetItem * lwi = new QListWidgetItem(QChar(c));
-               lwi->setTextAlignment(Qt::AlignCenter);
-               symbolsLW->addItem(lwi);
-               QString block = getBlock(c);
-               if (used_blocks.find(block) == used_blocks.end())
-                       used_blocks[block] = lwi;
+               QListWidgetItem * lwi = new QListWidgetItem(toqstr(c));
+               if (show_all || c >= range_start && c <= range_end) {
+                       lwi->setTextAlignment(Qt::AlignCenter);
+                       lwi->setToolTip(
+                               qt_("Character: ") + toqstr(c) + "\n" +
+                               qt_("Code Point: ") + getCodePoint(c)
+                               );
+                       symbolsLW->addItem(lwi);
+               }
+               if (update_combo) {
+                       QString block = getBlock(c);
+                       if (category.isEmpty())
+                               category = block;
+                       if (used_blocks.find(block) == used_blocks.end())
+                               used_blocks[block] = lwi;
+               }
        }
 
-       // update category combo
-       for (UsedBlocks::iterator it = used_blocks.begin(); it != used_blocks.end(); ++it) {
-               categoryCO->addItem(it->first);
+       if (update_combo) {
+               // update category combo
+               for (UsedBlocks::iterator it = used_blocks.begin();
+                    it != used_blocks.end(); ++it) {
+                       categoryCO->addItem(it->first);
+               }
        }
+
+       int old = categoryCO->findText(category);
+       if (old != -1)
+               categoryCO->setCurrentIndex(old);
+       // update again in case the combo has not yet been filled
+       // on first cycle (at dialog initialization)
+       if (nocategory && !category.isEmpty())
+               updateSymbolList();
 }
 
 
@@ -275,22 +366,9 @@ QString const GuiSymbols::getBlock(char_type c) const
 }
 
 
-
-bool GuiSymbols::initialiseParams(string const & data)
-{
-       if (data == encoding_)
-               // everything up to date
-               return true;
-       if (!data.empty())
-               encoding_ = data;
-       updateSymbolList();
-       return true;
-}
-
-
 void GuiSymbols::dispatchParams()
 {
-       dispatch(FuncRequest(LFUN_SELF_INSERT, fromqstr(chosenLE->text())));
+       dispatch(FuncRequest(getLfun(), fromqstr(chosenLE->text())));
 }