]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiToolbar.cpp
reduce line noise
[lyx.git] / src / frontends / qt4 / GuiToolbar.cpp
index 2539b89f905f17085608393b0cceadb33fd0762a..a7e1301a6473ebf1d57942870085002653162a2f 100644 (file)
@@ -23,6 +23,7 @@
 #include "IconPalette.h"
 #include "Layout.h"
 #include "LyXFunc.h"
+#include "TextClass.h"
 #include "ToolbarBackend.h"
 
 #include "GuiView.h"
@@ -32,6 +33,7 @@
 #include "Action.h"
 #include "qt_helpers.h"
 #include "InsertTableWidget.h"
+#include "LyXRC.h"
 
 #include "support/filetools.h"
 #include "support/lstrings.h"
 #include <QAction>
 #include <QPixmap>
 
+#include <boost/assert.hpp>
+
+
+static void initializeResources()
+{
+       static bool initialized = false;
+       if (!initialized) {
+               Q_INIT_RESOURCE(Resources); 
+               initialized = true;
+       }
+}
+
+
 namespace lyx {
+namespace frontend {
 
 using std::string;
 using std::endl;
 
-using support::FileName;
 using support::libFileSearch;
 using support::subst;
 using support::compare;
 
 
-namespace frontend {
-
-
 namespace {
 
 struct PngMap {
@@ -149,11 +161,9 @@ string const find_png(string const & name)
                png_name = subst(png_name, "!", "negthinspace");
        }
 
-       LYXERR(Debug::GUI) << "find_png(" << name << ")\n"
-                          << "Looking for math PNG called \""
-                          << png_name << '"' << std::endl;
-
-       return libFileSearch("images/math/", png_name, "png").absFilename();
+       LYXERR(Debug::GUI, "find_png(" << name << ")\n"
+               << "Looking for math PNG called \"" << png_name << '"');
+       return png_name;
 }
 
 } // namespace anon
@@ -162,44 +172,58 @@ string const find_png(string const & name)
 /// return a icon for the given action
 static QIcon getIcon(FuncRequest const & f, bool unknown)
 {
+       initializeResources();
+       QPixmap pm;
+       string name1;
+       string name2;
+       string path;
        string fullname;
 
        switch (f.action) {
        case LFUN_MATH_INSERT:
-               if (!f.argument().empty())
-                       fullname = find_png(to_utf8(f.argument()).substr(1));
+               if (!f.argument().empty()) {
+                       path = "math/";
+                       name1 = find_png(to_utf8(f.argument()).substr(1));
+               }
                break;
        case LFUN_MATH_DELIM:
        case LFUN_MATH_BIGDELIM:
-               fullname = find_png(to_utf8(f.argument()));
+               path = "math/";
+               name1 = find_png(to_utf8(f.argument()));
+               break;
+       case LFUN_CALL:
+               path = "commands/";
+               name1 = to_utf8(f.argument());
                break;
        default:
-               string const name = lyxaction.getActionName(f.action);
-               string png_name = name;
+               name2 = lyxaction.getActionName(f.action);
+               name1 = name2;
 
                if (!f.argument().empty())
-                       png_name = subst(name + ' ' + to_utf8(f.argument()), ' ', '_');
+                       name1 = subst(name2 + ' ' + to_utf8(f.argument()), ' ', '_');
+       }
 
-               fullname = libFileSearch("images", png_name, "png").absFilename();
+       fullname = libFileSearch("images/" + path, name1, "png").absFilename();
+       if (pm.load(toqstr(fullname)))
+               return pm;
 
-               if (fullname.empty()) {
-                       // try without the argument
-                       fullname = libFileSearch("images", name, "png").absFilename();
-               }
-       }
+       fullname = libFileSearch("images/" + path, name2, "png").absFilename();
+       if (pm.load(toqstr(fullname)))
+               return pm;
 
-       if (!fullname.empty()) {
-               LYXERR(Debug::GUI) << "Full icon name is `"
-                                  << fullname << '\'' << endl;
-               return QIcon(toqstr(fullname));
-       }
+       if (pm.load(":/images/" + toqstr(path + name1) + ".png"))
+               return pm;
+
+       if (pm.load(":/images/" + toqstr(path + name2) + ".png"))
+               return pm;
 
-       LYXERR(Debug::GUI) << "Cannot find icon for command \""
+       LYXERR(Debug::GUI, "Cannot find icon for command \""
                           << lyxaction.getActionName(f.action)
-                          << '(' << to_utf8(f.argument()) << ")\"" << endl;
+                          << '(' << to_utf8(f.argument()) << ")\"");
        if (unknown)
-               return QIcon(toqstr(libFileSearch("images", "unknown", "png").absFilename()));
-       return QIcon();
+               pm.load(":/images/unknown.png");
+
+       return pm;
 }
 
 
@@ -215,7 +239,7 @@ static TextClass const & textClass(LyXView const & lv)
 //
 /////////////////////////////////////////////////////////////////////
 
-GuiLayoutBox::GuiLayoutBox(GuiViewBase & owner)
+GuiLayoutBox::GuiLayoutBox(GuiView & owner)
        : owner_(owner)
 {
        setSizeAdjustPolicy(QComboBox::AdjustToContents);
@@ -250,6 +274,29 @@ void GuiLayoutBox::set(docstring const & layout)
 }
 
 
+void GuiLayoutBox::addItemSort(QString const & item, bool sorted)
+{
+       int const end = count();
+       if (!sorted || end < 2 || item[0].category() != QChar::Letter_Uppercase) {
+               addItem(item);
+               return;
+       }
+
+       // Let the default one be at the beginning
+       int i = 1;
+       for (setCurrentIndex(i); currentText() < item;) {
+               // e.g. --Separator--
+               if (currentText()[0].category() != QChar::Letter_Uppercase)
+                       break;
+               if (++i == end)
+                       break;
+               setCurrentIndex(i);
+       }
+
+       insertItem(i, item);
+}
+
+
 void GuiLayoutBox::updateContents()
 {
        TextClass const & tc = textClass(owner_);
@@ -261,10 +308,11 @@ void GuiLayoutBox::updateContents()
        TextClass::const_iterator const end = tc.end();
        for (; it != end; ++it) {
                // ignore obsolete entries
-               if ((*it)->obsoleted_by().empty())
-                       addItem(toqstr(translateIfPossible((*it)->name())));
+               addItemSort(toqstr(translateIfPossible((*it)->name())), lyxrc.sort_layouts);
        }
 
+       setCurrentIndex(0);
+
        // needed to recalculate size hint
        hide();
        setMinimumWidth(sizeHint().width());
@@ -290,8 +338,7 @@ void GuiLayoutBox::selected(const QString & str)
                        return;
                }
        }
-       lyxerr << "ERROR (layoutSelected): layout not found!"
-              << endl;
+       lyxerr << "ERROR (layoutSelected): layout not found!" << endl;
 }
 
 
@@ -303,7 +350,7 @@ void GuiLayoutBox::selected(const QString & str)
 /////////////////////////////////////////////////////////////////////
 
 
-GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiViewBase & owner)
+GuiToolbar::GuiToolbar(ToolbarInfo const & tbinfo, GuiView & owner)
        : QToolBar(qt_(tbinfo.gui_name), &owner), owner_(owner),
          layout_(0), command_buffer_(0)
 {
@@ -396,8 +443,7 @@ void GuiToolbar::add(ToolbarItem const & item)
                tb->setToolTip(qt_(to_ascii(item.label_)));
                tb->setStatusTip(qt_(to_ascii(item.label_)));
                tb->setText(qt_(to_ascii(item.label_)));
-               FileName icon_path = libFileSearch("images/math", item.name_, "png");
-               tb->setIcon(QIcon(toqstr(icon_path.absFilename())));
+               tb->setIcon(QPixmap(":images/math/" + toqstr(item.name_) + ".png"));
                connect(this, SIGNAL(iconSizeChanged(QSize)),
                        tb, SLOT(setIconSize(QSize)));
 
@@ -476,7 +522,6 @@ void GuiToolbar::updateContents()
 }
 
 
-
 } // namespace frontend
 } // namespace lyx