Spoiling some fun from Andre': put Application on a diet and remove unnecessary string conversions and method indirections.

git-svn-id: svn://svn.lyx.org/lyx/lyx-devel/trunk@21665 a592a061-630c-0410-9148-cb99ea01b6c8
This commit is contained in:
Abdelrazak Younes 2007-11-18 20:36:52 +00:00
parent 7bd8477a60
commit 1b147f12b0
11 changed files with 77 additions and 117 deletions

View File

@ -430,8 +430,6 @@ int LyX::exec(int & argc, char * argv[])
// Let the frontend parse and remove all arguments that it knows
pimpl_->application_.reset(createApplication(argc, argv));
initGuiFont();
// Parse and remove all known arguments in the LyX singleton
// Give an error for all remaining ones.
int exit_status = init(argc, argv);
@ -792,20 +790,6 @@ void LyX::printError(ErrorItem const & ei)
}
void LyX::initGuiFont()
{
if (lyxrc.roman_font_name.empty())
lyxrc.roman_font_name = pimpl_->application_->romanFontName();
if (lyxrc.sans_font_name.empty())
lyxrc.sans_font_name = pimpl_->application_->sansFontName();
if (lyxrc.typewriter_font_name.empty())
lyxrc.typewriter_font_name
= pimpl_->application_->typewriterFontName();
}
bool LyX::init()
{
#ifdef SIGHUP

View File

@ -133,9 +133,6 @@ private:
/// Create a View, load files and restore GUI Session.
void restoreGuiSession();
/// Initialize RC font for the GUI.
void initGuiFont();
/// initial LyX set up
bool init();
/// set up the default dead key bindings if requested

View File

@ -147,18 +147,16 @@ class Application
{
public:
///
Application() : current_view_(0) {}
Application() {}
///
virtual ~Application() {}
///
virtual bool closeAllViews() = 0;
///
virtual LyXView & view(int id) const = 0;
///
virtual size_t viewCount() const = 0;
///
virtual void hideDialogs(std::string const & name, Inset * inset) const = 0;
///
@ -176,22 +174,6 @@ public:
*/
virtual void exit(int status) = 0;
/**
* Synchronise all pending events.
*/
virtual void syncEvents() = 0;
///
virtual FontLoader & fontLoader() = 0;
/// return a suitable serif font name.
virtual std::string const romanFontName() = 0;
/// return a suitable sans serif font name.
virtual std::string const sansFontName() = 0;
/// return a suitable monospaced font name.
virtual std::string const typewriterFontName() = 0;
/**
* Given col, fills r, g, b in the range 0-255.
* The function returns true if successful.
@ -225,18 +207,6 @@ public:
/// Create the main window with given geometry settings.
/// \param geometry_arg: only for Windows platform.
virtual LyXView & createView(std::string const & geometry_arg) = 0;
///
LyXView const * currentView() const { return current_view_; }
///
LyXView * currentView() { return current_view_; }
///
void setCurrentView(LyXView & view) { current_view_ = &view; }
protected:
/// This LyXView is the one receiving Clipboard and Selection
/// events
LyXView * current_view_;
};
} // namespace frontend

View File

@ -125,7 +125,7 @@ int prompt(docstring const & title0, docstring const & question,
// This call has no effect if the cursor has not been overridden.
qApp->changeOverrideCursor(Qt::ArrowCursor);
// FIXME replace that with theApp->gui()->currentView()
// FIXME replace that with guiApp->currentView()
int res = QMessageBox::information(qApp->focusWidget(),
toqstr(title),
toqstr(formatted(question)),

View File

@ -136,7 +136,7 @@ GuiApplication * guiApp;
GuiApplication::GuiApplication(int & argc, char ** argv)
: QApplication(argc, argv), Application()
: QApplication(argc, argv), Application(), current_view_(0)
{
QString app_name = "LyX";
QCoreApplication::setOrganizationName(app_name);
@ -194,6 +194,16 @@ GuiApplication::GuiApplication(int & argc, char ** argv)
// Set the cache to 5120 kilobytes which corresponds to screen size of
// 1280 by 1024 pixels with a color depth of 32 bits.
QPixmapCache::setCacheLimit(5120);
// Initialize RC Fonts
if (lyxrc.roman_font_name.empty())
lyxrc.roman_font_name = fromqstr(romanFontName());
if (lyxrc.sans_font_name.empty())
lyxrc.sans_font_name = fromqstr(sansFontName());
if (lyxrc.typewriter_font_name.empty())
lyxrc.typewriter_font_name = fromqstr(typewriterFontName());
}
@ -203,9 +213,24 @@ GuiApplication::~GuiApplication()
}
static void updateIds(map<int, GuiView *> const & stdmap, vector<int> & ids)
{
ids.clear();
map<int, GuiView *>::const_iterator it;
for (it = stdmap.begin(); it != stdmap.end(); ++it)
ids.push_back(it->first);
}
LyXView & GuiApplication::createView(string const & geometry_arg)
{
int const id = createRegisteredView();
updateIds(views_, view_ids_);
int id = 0;
while (views_.find(id) != views_.end())
id++;
views_[id] = new GuiView(id);
updateIds(views_, view_ids_);
GuiView * view = views_[id];
theLyXFunc().setLyXView(view);
@ -265,36 +290,36 @@ void GuiApplication::execBatchCommands()
}
string const GuiApplication::romanFontName()
QString const GuiApplication::romanFontName()
{
QFont font;
font.setKerning(false);
font.setStyleHint(QFont::Serif);
font.setFamily("serif");
return fromqstr(QFontInfo(font).family());
return QFontInfo(font).family();
}
string const GuiApplication::sansFontName()
QString const GuiApplication::sansFontName()
{
QFont font;
font.setKerning(false);
font.setStyleHint(QFont::SansSerif);
font.setFamily("sans");
return fromqstr(QFontInfo(font).family());
return QFontInfo(font).family();
}
string const GuiApplication::typewriterFontName()
QString const GuiApplication::typewriterFontName()
{
QFont font;
font.setKerning(false);
font.setStyleHint(QFont::TypeWriter);
font.setFamily("monospace");
return fromqstr(QFontInfo(font).family());
return QFontInfo(font).family();
}
@ -305,7 +330,7 @@ bool GuiApplication::event(QEvent * e)
// Open a file; this happens only on Mac OS X for now
QFileOpenEvent * foe = static_cast<QFileOpenEvent *>(e);
if (!currentView() || !currentView()->view())
if (!current_view_ || !current_view_->view())
// The application is not properly initialized yet.
// So we acknowledge the event and delay the file opening
// until LyX is ready.
@ -360,16 +385,6 @@ bool GuiApplication::notify(QObject * receiver, QEvent * event)
}
void GuiApplication::syncEvents()
{
// This is the ONLY place where processEvents may be called.
// During screen update/ redraw, this method is disabled to
// prevent keyboard events being handed to the LyX core, where
// they could cause re-entrant calls to screen update.
processEvents(QEventLoop::ExcludeUserInputEvents);
}
bool GuiApplication::getRgbColor(ColorCode col, RGBColor & rgbcol)
{
QColor const & qcol = color_cache_.get(col);
@ -438,27 +453,6 @@ void GuiApplication::addMenuTranslator()
}
static void updateIds(map<int, GuiView *> const & stdmap, vector<int> & ids)
{
ids.clear();
map<int, GuiView *>::const_iterator it;
for (it = stdmap.begin(); it != stdmap.end(); ++it)
ids.push_back(it->first);
}
int GuiApplication::createRegisteredView()
{
updateIds(views_, view_ids_);
int id = 0;
while (views_.find(id) != views_.end())
id++;
views_[id] = new GuiView(id);
updateIds(views_, view_ids_);
return id;
}
bool GuiApplication::unregisterView(int id)
{
updateIds(views_, view_ids_);
@ -537,7 +531,7 @@ Buffer const * GuiApplication::updateInset(Inset const * inset) const
#ifdef Q_WS_X11
bool GuiApplication::x11EventFilter(XEvent * xev)
{
if (!currentView())
if (!current_view_)
return false;
switch (xev->type) {
@ -545,7 +539,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
if (xev->xselectionrequest.selection != XA_PRIMARY)
break;
LYXERR(Debug::GUI, "X requested selection.");
BufferView * bv = currentView()->view();
BufferView * bv = current_view_->view();
if (bv) {
docstring const sel = bv->requestSelection();
if (!sel.empty())
@ -557,7 +551,7 @@ bool GuiApplication::x11EventFilter(XEvent * xev)
if (xev->xselectionclear.selection != XA_PRIMARY)
break;
LYXERR(Debug::GUI, "Lost selection.");
BufferView * bv = currentView()->view();
BufferView * bv = current_view_->view();
if (bv)
bv->clearSelection();
break;
@ -583,8 +577,8 @@ frontend::FontLoader & theFontLoader()
if (!use_gui)
return no_gui_font_loader;
BOOST_ASSERT(theApp());
return theApp()->fontLoader();
BOOST_ASSERT(frontend::guiApp);
return frontend::guiApp->fontLoader();
}
@ -601,8 +595,8 @@ frontend::FontMetrics const & theFontMetrics(FontInfo const & f)
if (!use_gui)
return no_gui_font_metrics;
BOOST_ASSERT(theApp());
return theApp()->fontLoader().metrics(f);
BOOST_ASSERT(frontend::guiApp);
return frontend::guiApp->fontLoader().metrics(f);
}

View File

@ -62,10 +62,6 @@ public:
virtual int exec();
virtual void exit(int status);
virtual bool event(QEvent * e);
void syncEvents();
virtual std::string const romanFontName();
virtual std::string const sansFontName();
virtual std::string const typewriterFontName();
virtual bool getRgbColor(ColorCode col, RGBColor & rgbcol);
virtual std::string const hexName(ColorCode col);
virtual void updateColor(ColorCode col);
@ -80,7 +76,15 @@ public:
void commitData(QSessionManager & sm);
//@}
///
GuiView const * currentView() const { return current_view_; }
///
GuiView * currentView() { return current_view_; }
///
void setCurrentView(GuiView & view) { current_view_ = &view; }
///
virtual size_t viewCount() const { return view_ids_.size(); }
///
std::vector<int> const & viewIds() { return view_ids_; }
///
@ -88,11 +92,19 @@ public:
///
GuiFontLoader & guiFontLoader() { return font_loader_; }
/// return a suitable serif font name.
virtual QString const romanFontName();
virtual int createRegisteredView();
/// return a suitable sans serif font name.
virtual QString const sansFontName();
/// return a suitable monospaced font name.
virtual QString const typewriterFontName();
///
virtual bool closeAllViews();
///
virtual bool unregisterView(int id);
///
virtual LyXView & view(int id) const;
///
virtual void hideDialogs(std::string const & name, Inset * inset) const;
@ -136,6 +148,9 @@ public:
std::map<int, GuiView *> views_;
///
std::vector<int> view_ids_;
/// This LyXView is the one receiving Clipboard and Selection
/// events
GuiView * current_view_;
}; // GuiApplication
extern GuiApplication * guiApp;

View File

@ -13,12 +13,11 @@
#include "GuiLog.h"
#include "GuiApplication.h"
#include "qt_helpers.h"
#include "gettext.h"
#include "Lexer.h"
#include "frontends/Application.h"
#include <QCloseEvent>
#include <QTextBrowser>
#include <QSyntaxHighlighter>
@ -117,7 +116,7 @@ GuiLog::GuiLog(LyXView & lv)
highlighter = new LogHighlighter(logTB->document());
logTB->setReadOnly(true);
QFont font(toqstr(theApp()->typewriterFontName()));
QFont font(guiApp->typewriterFontName());
font.setKerning(false);
font.setFixedPitch(true);
font.setStyleHint(QFont::TypeWriter);

View File

@ -161,15 +161,16 @@ static void setComboxFont(QComboBox * cb, string const & family,
QFont font;
font.setKerning(false);
if (family == theApp()->romanFontName()) {
QString const font_family = toqstr(family);
if (font_family == guiApp->romanFontName()) {
font.setStyleHint(QFont::Serif);
font.setFamily(family.c_str());
} else if (family == theApp()->sansFontName()) {
font.setFamily(font_family);
} else if (font_family == guiApp->sansFontName()) {
font.setStyleHint(QFont::SansSerif);
font.setFamily(family.c_str());
} else if (family == theApp()->typewriterFontName()) {
font.setFamily(font_family);
} else if (font_family == guiApp->typewriterFontName()) {
font.setStyleHint(QFont::TypeWriter);
font.setFamily(family.c_str());
font.setFamily(font_family);
} else {
lyxerr << "FAILED to find the default font: '"
<< foundry << "', '" << family << '\''<< endl;

View File

@ -12,11 +12,11 @@
#include <config.h>
#include "GuiApplication.h"
#include "GuiViewSource.h"
#include "LaTeXHighlighter.h"
#include "qt_helpers.h"
#include "Application.h"
#include "BufferView.h"
#include "Buffer.h"
#include "Cursor.h"
@ -57,7 +57,7 @@ ViewSourceWidget::ViewSourceWidget(GuiViewSource & controller)
viewSourceTV->setReadOnly(true);
///dialog_->viewSourceTV->setAcceptRichText(false);
// this is personal. I think source code should be in fixed-size font
QFont font(toqstr(theApp()->typewriterFontName()));
QFont font(guiApp->typewriterFontName());
font.setKerning(false);
font.setFixedPitch(true);
font.setStyleHint(QFont::TypeWriter);

View File

@ -304,7 +304,7 @@ void GuiWorkArea::redraw()
// No need to do anything if this is the current view. The BufferView
// metrics are already up to date.
if (lyx_view_ != theApp()->currentView()) {
if (lyx_view_ != guiApp->currentView()) {
// FIXME: it would be nice to optimize for the off-screen case.
buffer_view_->updateMetrics();
buffer_view_->cursor().fixIfBroken();

View File

@ -48,7 +48,7 @@ LyXFileDialog::LyXFileDialog(docstring const & t,
support::FileFilterList const & filters,
FileDialog::Button const & b1,
FileDialog::Button const & b2)
// FIXME replace that with theApp->gui()->currentView()
// FIXME replace that with guiApp->currentView()
: QFileDialog(qApp->focusWidget(),
toqstr(t), toqstr(p), toqstr(filters.as_string()))
{