]> git.lyx.org Git - lyx.git/blobdiff - src/frontends/qt4/GuiTexinfo.cpp
do what the FIXME suggested
[lyx.git] / src / frontends / qt4 / GuiTexinfo.cpp
index 8070013bd2483d13623658891687c74cdfdd8582..b718d4b28788e22f62470c74e047ec852668181e 100644 (file)
@@ -4,6 +4,7 @@
  * Licence details can be found in the file COPYING.
  *
  * \author Edwin Leuven
+ * \author Herbert Voß
  *
  * Full author contact details are available in file CREDITS.
  */
 #include <config.h>
 
 #include "GuiTexinfo.h"
-#include "Qt2BC.h"
-#include "qt_helpers.h"
 
+#include "FuncRequest.h"
+
+#include "support/debug.h"
 #include "support/filetools.h"
+#include "support/FileName.h"
+#include "support/lstrings.h"
+
+#include "qt_helpers.h"
 
 #include <QCheckBox>
 #include <QListWidget>
 #include <QPushButton>
 
-using std::string;
-using std::vector;
+#include <fstream>
+#include <algorithm>
+
+using namespace std;
+using namespace lyx::support;
 
 namespace lyx {
 namespace frontend {
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiTexinfoDialog
-//
-/////////////////////////////////////////////////////////////////////
 
+static string texFileFromList(string const & file, string const & type)
+{
+       string file_ = file;
+       // do we need to add the suffix?
+       if (!(getExtension(file) == type))
+               file_ += '.' + type;
+
+       lyxerr << "Searching for file " << file_ << endl;
+
+       string lstfile = type + "Files.lst";
+       if (type == "cls")
+               lstfile = "clsFiles.lst";
+       else if (type == "sty")
+               lstfile = "styFiles.lst";
+       else if (type == "bst")
+               lstfile = "bstFiles.lst";
+       else if (type == "bib")
+               lstfile = "bibFiles.lst";
+       FileName const abslstfile = libFileSearch(string(), lstfile);
+       if (abslstfile.empty()) {
+               lyxerr << "File `'" << lstfile << "' not found." << endl;
+               return string();
+       }
+       // FIXME UNICODE
+       string const allClasses = to_utf8(abslstfile.fileContents("UTF-8"));
+       int entries = 0;
+       string classfile = token(allClasses, '\n', entries);
+       int count = 0;
+       while ((!contains(classfile, file) ||
+               (onlyFilename(classfile) != file)) &&
+               (++count < 1000)) {
+               classfile = token(allClasses, '\n', ++entries);
+       }
 
-GuiTexinfoDialog::GuiTexinfoDialog(GuiTexinfo * form)
-       : form_(form)
+       // now we have filename with full path
+       lyxerr << "with full path: " << classfile << endl;
+
+       return classfile;
+}
+
+
+GuiTexInfo::GuiTexInfo(GuiView & lv)
+       : GuiDialog(lv, "texinfo", qt_("TeX Information"))
 {
        setupUi(this);
 
-       connect(closePB, SIGNAL(clicked()), form, SLOT(slotClose()));
+       warningPosted = false;
+       activeStyle = ClsType;
+
+       connect(closePB, SIGNAL(clicked()), this, SLOT(slotClose()));
 
        connect(viewPB, SIGNAL(clicked()), this, SLOT(viewClicked()));
-       connect(whatStyleCO, SIGNAL(activated(const QString &)),
+       connect(whatStyleCO, SIGNAL(activated(QString)),
                this, SLOT(enableViewPB()));
-       connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(update()));
-       connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(update()));
+       connect(whatStyleCO, SIGNAL(activated(int)), this, SLOT(updateView()));
+       connect(pathCB, SIGNAL(stateChanged(int)), this, SLOT(updateView()));
        connect(rescanPB, SIGNAL(clicked()), this, SLOT(enableViewPB()));
        connect(rescanPB, SIGNAL(clicked()), this, SLOT(rescanClicked()));
        connect(fileListLW, SIGNAL(itemClicked(QListWidgetItem *)),
-               this, SLOT( enableViewPB() ) );
+               this, SLOT(enableViewPB()));
        connect(fileListLW, SIGNAL(itemSelectionChanged()),
                this, SLOT(enableViewPB()));
-}
 
+       updateStyles(ClsType);
 
-void GuiTexinfoDialog::change_adaptor()
-{
-       form_->changed();
+       bc().setPolicy(ButtonPolicy::OkCancelPolicy);
+       bc().setCancel(closePB);
 }
 
 
-void GuiTexinfoDialog::closeEvent(QCloseEvent * e)
+void GuiTexInfo::change_adaptor()
 {
-       form_->slotWMHide();
-       e->accept();
+       changed();
 }
 
 
-void GuiTexinfoDialog::rescanClicked()
+void GuiTexInfo::rescanClicked()
 {
        // build new *Files.lst
        rescanTexStyles();
-       form_->updateStyles();
+       updateStyles();
        enableViewPB();
 }
 
 
-void GuiTexinfoDialog::viewClicked()
+void GuiTexInfo::viewClicked()
 {
        size_t const fitem = fileListLW->currentRow();
-       vector<string> const & data = form_->texdata_[form_->activeStyle];
+       vector<string> const & data = texdata_[activeStyle];
        string file = data[fitem];
        if (!pathCB->isChecked())
-               file = getTexFileFromList(data[fitem],
-                       form_->controller().getFileType(form_->activeStyle));
-       form_->controller().viewFile(file);
+               file = texFileFromList(data[fitem], fileType(activeStyle));
+       viewFile(file);
 }
 
 
-void GuiTexinfoDialog::update()
+void GuiTexInfo::updateView()
 {
-       switch (whatStyleCO->currentIndex()) {
-       case 0:
-               form_->updateStyles(ControlTexinfo::cls);
-               break;
-       case 1:
-               form_->updateStyles(ControlTexinfo::sty);
-               break;
-       case 2:
-               form_->updateStyles(ControlTexinfo::bst);
-               break;
-       default:
-               break;
-       }
-
+       // takes advantage of enum order
+       updateStyles(static_cast<TexFileType>(whatStyleCO->currentIndex()));
        enableViewPB();
 }
 
 
-void GuiTexinfoDialog::enableViewPB()
+void GuiTexInfo::enableViewPB()
 {
        viewPB->setEnabled(fileListLW->currentRow() > -1);
 }
 
 
+void GuiTexInfo::updateStyles(TexFileType type)
+{
+       ContentsType & data = texdata_[type];
+
+       static string filenames[] = { "clsFile.lst", "styFiles.lst", "bstFiles.lst" };
+       string filename = filenames[type];
+
+       getTexFileList(filename, data);
+       if (data.empty()) {
+               // build filelists of all availabe bst/cls/sty-files.
+               // Done through kpsewhich and an external script,
+               // saved in *Files.lst
+               rescanTexStyles();
+               getTexFileList(filename, data);
+       }
+       bool const withFullPath = pathCB->isChecked();
+       if (withFullPath)
+               return;
+       vector<string>::iterator it1  = data.begin();
+       vector<string>::iterator end1 = data.end();
+       for (; it1 != end1; ++it1)
+               *it1 = onlyFilename(*it1);
+
+       // sort on filename only (no path)
+       sort(data.begin(), data.end());
+
+       fileListLW->clear();
+       ContentsType::const_iterator it  = data.begin();
+       ContentsType::const_iterator end = data.end();
+       for (; it != end; ++it)
+               fileListLW->addItem(toqstr(*it));
 
-/////////////////////////////////////////////////////////////////////
-//
-// GuiTexinfo
-//
-/////////////////////////////////////////////////////////////////////
+       activeStyle = type;
+}
 
-typedef QController<ControlTexinfo, GuiView<GuiTexinfoDialog> >
-       texinfo_base_class;
 
-GuiTexinfo::GuiTexinfo(Dialog & parent)
-       : texinfo_base_class(parent, _("TeX Information")),
-         warningPosted(false), activeStyle(ControlTexinfo::cls)
+void GuiTexInfo::updateStyles()
 {
+       updateStyles(activeStyle);
 }
 
 
-void GuiTexinfo::build_dialog()
+void GuiTexInfo::viewFile(string const & filename) const
 {
-       dialog_.reset(new GuiTexinfoDialog(this));
-
-       updateStyles(ControlTexinfo::cls);
-
-       bcview().setCancel(dialog_->closePB);
+       dispatch(FuncRequest(LFUN_DIALOG_SHOW, "file " + filename));
 }
 
 
-void GuiTexinfo::updateStyles(ControlTexinfo::texFileSuffix whichStyle)
+/// get a class with full path from the list
+string GuiTexInfo::classOptions(string const & classname) const
 {
-       ContentsType & data = texdata_[whichStyle];
-       bool const withFullPath = dialog_->pathCB->isChecked();
-
-       getTexFileList(whichStyle, data, withFullPath);
-
-       dialog_->fileListLW->clear();
-       ContentsType::const_iterator it  = data.begin();
-       ContentsType::const_iterator end = data.end();
-       for (; it != end; ++it)
-               dialog_->fileListLW->addItem(toqstr(*it));
-
-       activeStyle = whichStyle;
+       FileName const filename(texFileFromList(classname, "cls"));
+       if (filename.empty())
+               return string();
+       string optionList;
+       ifstream is(filename.toFilesystemEncoding().c_str());
+       while (is) {
+               string s;
+               is >> s;
+               if (contains(s, "DeclareOption")) {
+                       s = s.substr(s.find("DeclareOption"));
+                       s = split(s, '{');              // cut front
+                       s = token(s, '}', 0);           // cut end
+                       optionList += (s + '\n');
+               }
+       }
+       return optionList;
 }
 
 
-void GuiTexinfo::updateStyles()
+string GuiTexInfo::fileType(TexFileType type) const
 {
-       updateStyles(activeStyle);
+       // takes advantage of enum order
+       static string const ext[] = { "cls", "sty", "bst" };
+       return ext[type];
 }
 
+
+Dialog * createGuiTexInfo(GuiView & lv) { return new GuiTexInfo(lv); }
+
+
 } // namespace frontend
 } // namespace lyx