]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiSymbols.cpp
Use <cstdint> instead of <boost/cstdint.hpp>
[lyx.git] / src / frontends / qt4 / GuiSymbols.cpp
index 2aadfff79f5eb5761c8ff7a194c1e0745d12c14d..ea863b49686fb03137119c61654346e836582305 100644 (file)
@@ -27,6 +27,8 @@
 #include "support/gettext.h"
 
 #include <QChar>
+#include <QDialogButtonBox>
+#include <QPushButton>
 #include <QString>
 
 #include <cstdio>
@@ -152,6 +154,7 @@ const int no_blocks = sizeof(unicode_blocks) / sizeof(UnicodeBlocks);
 QString getBlock(char_type c)
 {
        // store an educated guess for the next search
+       // 0 <= lastBlock < no_blocks
        // FIXME THREAD
        static int lastBlock = 0;
 
@@ -159,10 +162,6 @@ QString getBlock(char_type c)
        if (c < 0x7f)
                lastBlock = 0;
 
-       // off the end already
-       if (lastBlock == no_blocks)
-               return QString();
-
        // c falls into a covered area, and we can guess which
        if (c >= unicode_blocks[lastBlock].start
            && c <= unicode_blocks[lastBlock].end)
@@ -177,15 +176,25 @@ QString getBlock(char_type c)
        int i = 0;
        while (i < no_blocks && c > unicode_blocks[i].end)
                ++i;
+
        if (i == no_blocks)
                return QString();
+
+       if (c < unicode_blocks[i].start) {
+               // 0 < i < no_blocks
+               // cache the previous block for guessing next time
+               lastBlock = i - 1;
+               return QString();
+       }
+
+       // 0 <= i < no_blocks
+       // cache the last block for guessing next time
        lastBlock = i;
-       //LYXERR0("fail: " << int(c) << ' ' << lastBlock);
        return unicode_blocks[lastBlock].qname;
 }
 
 
-} // namespace anon
+} // namespace
 
 
 /////////////////////////////////////////////////////////////////////
@@ -198,7 +207,7 @@ class GuiSymbols::Model : public QAbstractListModel
 {
 public:
        Model(GuiSymbols * parent)
-               : QAbstractListModel(parent)
+               : QAbstractListModel(parent), encoding_(0)
        {}
 
        QModelIndex index(int row, int column, QModelIndex const &) const
@@ -221,9 +230,6 @@ public:
                if (!index.isValid())
                        return QVariant();
 
-               static QString const strCharacter = qt_("Character: ");
-               static QString const strCodePoint = qt_("Code Point: ");
-
                char_type c = symbols_.at(index.row());
 
                switch (role) {
@@ -232,10 +238,20 @@ public:
                case Qt::DisplayRole:
                        return toqstr(c);
                case Qt::ToolTipRole: {
-                       char codeName[10];
-                       sprintf(codeName, "0x%04x", c);
-                       return strCharacter + toqstr(c) + '\n'
-                               + strCodePoint + QLatin1String(codeName);
+                       QString latex;
+                       if (encoding_) {
+                               // how is the character output in the current encoding?
+                               docstring const code = encoding_->latexChar(c).first;
+                               // only show it when it is not coded by itself
+                               if (code != docstring(1, c))
+                                       latex = qt_("<p>LaTeX code: %1</p>").arg(toqstr(code));
+                       }
+                       return formatToolTip(QString("<p align=center><span "
+                                                    "style=\"font-size: xx-large;\">%1"
+                                                    "</span><br>U+%2</p>%3")
+                                            .arg(toqstr(c))
+                                            .arg(QString("%1").arg(c, 0, 16).toUpper())
+                                            .arg(latex));
                }
                case Qt::SizeHintRole:
                        // Fix many symbols not displaying in combination with
@@ -246,12 +262,11 @@ public:
                }
        }
 
-       void setSymbols(QList<char_type> const & symbols)
+       void setSymbols(QList<char_type> const & symbols, Encoding const * encoding)
        {
                beginResetModel();
-               beginInsertRows(QModelIndex(), 0, symbols.size() - 1);
                symbols_ = symbols;
-               endInsertRows();
+               encoding_ = encoding;
                endResetModel();
        }
 
@@ -259,6 +274,7 @@ private:
        friend class GuiSymbols;
 
        QList<char_type> symbols_;
+       Encoding const * encoding_;
 };
 
 
@@ -274,7 +290,7 @@ GuiSymbols::GuiSymbols(GuiView & lv)
 {
        setupUi(this);
 
-       //Translate names
+       // Translate names
        for (int i = 0 ; i < no_blocks; ++i)
                unicode_blocks[i].qname = qt_(unicode_blocks[i].name);
 
@@ -305,8 +321,8 @@ void GuiSymbols::updateView()
        chosenLE->clear();
 
        string new_encoding = bufferview()->cursor().getEncoding()->name();
-       if (buffer().params().inputenc != "auto" &&
-           buffer().params().inputenc != "default")
+       if (buffer().params().inputenc != "auto-legacy" &&
+           buffer().params().inputenc != "auto-legacy-plain")
                new_encoding = buffer().params().encoding().name();
        if (new_encoding == encoding_)
                // everything up to date
@@ -324,47 +340,58 @@ void GuiSymbols::updateView()
 void GuiSymbols::enableView(bool enable)
 {
        chosenLE->setEnabled(enable);
-       okPB->setEnabled(enable);
-       applyPB->setEnabled(enable);
+       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enable);
+       buttonBox->button(QDialogButtonBox::Apply)->setEnabled(enable);
+       if (enable)
+               buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Cancel"));
+       else
+               buttonBox->button(QDialogButtonBox::Close)->setText(qt_("Close"));
 }
 
 
-void GuiSymbols::on_applyPB_clicked()
+void GuiSymbols::on_buttonBox_clicked(QAbstractButton * button)
 {
-       dispatchParams();
+       QDialogButtonBox * bbox = qobject_cast<QDialogButtonBox*>(sender());
+       switch (bbox->standardButton(button)) {
+       case QDialogButtonBox::Ok:
+               slotOK();
+               break;
+       case QDialogButtonBox::Apply:
+               dispatchParams();
+               break;
+       case QDialogButtonBox::Close:
+               hide();
+               break;
+       default:
+               break;
+       }
 }
 
 
-void GuiSymbols::on_okPB_clicked()
+void GuiSymbols::slotOK()
 {
        dispatchParams();
        hide();
 }
 
 
-void GuiSymbols::on_closePB_clicked()
-{
-       hide();
-}
-
-
 void GuiSymbols::on_symbolsLW_activated(QModelIndex const &)
 {
-       on_okPB_clicked();
+       slotOK();
 }
 
 
 void GuiSymbols::on_chosenLE_textChanged(QString const & text)
 {
        bool const empty_sel = text.isEmpty();
-       okPB->setEnabled(!empty_sel);
-       applyPB->setEnabled(!empty_sel);
+       buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!empty_sel);
+       buttonBox->button(QDialogButtonBox::Apply)->setEnabled(!empty_sel);
 }
 
 
 void GuiSymbols::on_chosenLE_returnPressed()
 {
-       on_okPB_clicked();
+       slotOK();
 }
 
 
@@ -411,8 +438,7 @@ void GuiSymbols::scrollToItem(QString const & category)
 
 void GuiSymbols::updateSymbolList(bool update_combo)
 {
-       QString category = categoryCO->currentText();
-       bool const nocategory = category.isEmpty();
+       QString const category = categoryCO->currentText();
        char_type range_start = 0x0000;
        char_type range_end = 0x110000;
        QList<char_type> s;
@@ -422,8 +448,10 @@ void GuiSymbols::updateSymbolList(bool update_combo)
        }
        bool const show_all = categoryFilterCB->isChecked();
 
+       Encoding const * const enc = encodings.fromLyXName(encoding_);
+
        if (symbols_.empty() || update_combo)
-               symbols_ = encodings.fromLyXName(encoding_)->symbolsList();
+               symbols_ = enc->symbolsList();
 
        if (!show_all) {
                for (int i = 0 ; i < no_blocks; ++i)
@@ -434,11 +462,9 @@ void GuiSymbols::updateSymbolList(bool update_combo)
                        }
        }
 
-       SymbolsList::const_iterator const end = symbols_.end();
        int numItem = 0;
-       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))
+       for (char_type c : symbols_) {
+               if (!update_combo && !show_all && (c < range_start || c > range_end))
                        continue;
                QChar::Category const cat = QChar::category(uint(c));
                // we do not want control or space characters
@@ -449,13 +475,11 @@ void GuiSymbols::updateSymbolList(bool update_combo)
                        s.append(c);
                if (update_combo) {
                        QString block = getBlock(c);
-                       if (category.isEmpty())
-                               category = block;
                        if (used_blocks.find(block) == used_blocks.end())
                                used_blocks[block] = numItem;
                }
        }
-       model_->setSymbols(s);
+       model_->setSymbols(s, enc);
 
        if (update_combo) {
                // update category combo
@@ -468,10 +492,12 @@ void GuiSymbols::updateSymbolList(bool update_combo)
        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();
+       else if (update_combo) {
+               // restart with a non-empty block
+               // this happens when the encoding changes when moving the cursor
+               categoryCO->setCurrentIndex(0);
+               updateSymbolList(false);
+       }
 }