Back to index

texmacs  1.0.7.15
Defines | Functions | Variables
qt_utilities.cpp File Reference
#include "qt_utilities.hpp"
#include <QImage>
#include <QPrinter>
#include <QPainter>
#include <QCoreApplication>
#include <QLocale>
#include <QDateTime>
#include <QTextCodec>
#include <QHash>
#include <QStringList>
#include <QPrintDialog>
#include "rgb_colors.hpp"
#include "dictionary.hpp"
#include "converter.hpp"
#include "language.hpp"
#include "qt_gui.hpp"

Go to the source code of this file.

Defines

#define PAPER(fmt)   case QPrinter::fmt : return "fmt"

Functions

tm_ostreamoperator<< (tm_ostream &out, QRect rect)
 some debugging infrastucture
QRect to_qrect (const coord4 &p)
QPoint to_qpoint (const coord2 &p)
QSize to_qsize (const coord2 &p)
QString parse_tm_style (int style)
QString to_qstylesheet (int style)
QString to_qstylesheet (int style, color c)
QSize qt_decode_length (string width, QWidget *qwid)
 Try to convert a TeXmacs lenght (em, px, w, h) in a QSize.
coord4 from_qrect (const QRect &rect)
coord2 from_qpoint (const QPoint &pt)
coord2 from_qsize (const QSize &s)
QStringList to_qstringlist (array< string > l)
array< stringfrom_qstringlist (const QStringList &l)
QString to_qstring (string s)
QString utf8_to_qstring (string s)
string from_qstring_utf8 (const QString &s)
string from_qstring (const QString &s)
void initNamedColors (void)
 This needn't be called more than once.
QColor to_qcolor (const string &col)
 Takes either an hexadecimal RGB color, as in #e3a1ff, or a named color as those defined in src/Graphics/Renderer/rgb_colors.hpp and returns a QColor.
string from_qcolor (const QColor &col)
 Returns a color encoded as a string with hexadecimal RGB values, as in #e3a1ff.
QColor to_qcolor (color c)
string qt_translate (string s)
 convert a QString to a TeXmacs cork string
bool qt_supports (url u)
void qt_image_size (url image, int &w, int &h)
void qt_convert_image (url image, url dest, int w, int h)
void qt_image_to_eps (url image, url eps, int w_pt, int h_pt, int dpi)
string qt_application_directory ()
string qt_get_date (string lan, string fm)
static string qt_papersize_to_string (QPrinter::PaperSize sz)
bool qt_print (bool &to_file, bool &landscape, string &pname, url &filename, string &first, string &last, string &paper_type)

Variables

static QHash< QString, QColor > _NamedColors

Define Documentation

#define PAPER (   fmt)    case QPrinter::fmt : return "fmt"

Definition at line 400 of file qt_utilities.cpp.


Function Documentation

string from_qcolor ( const QColor &  col)

Returns a color encoded as a string with hexadecimal RGB values, as in #e3a1ff.

Definition at line 252 of file qt_utilities.cpp.

                                {
  return from_qstring(col.name());
}

Here is the call graph for this function:

Here is the caller graph for this function:

coord2 from_qpoint ( const QPoint &  pt)

Definition at line 151 of file qt_utilities.cpp.

                                {
  SI c1, c2;
  c1= pt.x() * PIXEL;
  c2= -pt.y() * PIXEL;
  return coord2 (c1, c2);
}

Here is the caller graph for this function:

coord4 from_qrect ( const QRect &  rect)

Definition at line 141 of file qt_utilities.cpp.

                                {
  SI c1, c2, c3, c4;
  c1= rect.x() * PIXEL;
  c2= -(rect.y() + rect.height()) * PIXEL;       
  c3= (rect.x() + rect.width()) * PIXEL;
  c4= -rect.y() * PIXEL;
  return coord4 (c1, c2, c3, c4);
}

Here is the caller graph for this function:

coord2 from_qsize ( const QSize &  s)

Definition at line 159 of file qt_utilities.cpp.

                             {
  SI c1, c2;
  c1= s.width() * PIXEL;
  c2= s.height() * PIXEL;
  return coord2 (c1, c2);
}

Here is the caller graph for this function:

string from_qstring ( const QString &  s)

Definition at line 214 of file qt_utilities.cpp.

                                {
  return utf8_to_cork (from_qstring_utf8(s));
}

Here is the call graph for this function:

Here is the caller graph for this function:

string from_qstring_utf8 ( const QString &  s)

Definition at line 207 of file qt_utilities.cpp.

                                     {
  QByteArray arr= s.toUtf8 ();
  const char* cstr= arr.constData ();
  return string ((char*) cstr);
}

Here is the caller graph for this function:

array<string> from_qstringlist ( const QStringList &  l)

Definition at line 175 of file qt_utilities.cpp.

                                       {
  array<string> tl (l.size());
  for(QStringList::const_iterator it = l.begin(); it != l.end(); ++it)
    tl << from_qstring(*it);
  return tl;
}

Here is the call graph for this function:

void initNamedColors ( void  )

This needn't be called more than once.

Takes RGBColors, defined in rgb_colors.hpp and initializes our QHash

Definition at line 226 of file qt_utilities.cpp.

                           {
  for(int i = 0; i < RGBColorsSize; ++i)
    _NamedColors.insert(QString(RGBColors[i].name), 
                        QColor(RGBColors[i].r, RGBColors[i].g, RGBColors[i].b));
}

Here is the caller graph for this function:

tm_ostream& operator<< ( tm_ostream out,
QRect  rect 
)

some debugging infrastucture

Definition at line 40 of file qt_utilities.cpp.

                                          {
  return out << "(" << rect.x() << "," << rect.y() << ","
  << rect.width() << "," << rect.height() << ")";
}
QString parse_tm_style ( int  style)

Definition at line 65 of file qt_utilities.cpp.

                          {
  QString sheet;
  if (style & WIDGET_STYLE_MINI)  // use smaller text font inside widget
    sheet += "font-size: 10pt;";
  if (style & WIDGET_STYLE_MONOSPACED)  // use monospaced font inside widget
    sheet += "font-family: \"monospace\";";
  if (style & WIDGET_STYLE_GREY)  // use grey text font
    sheet += "font-color: #eeeeee";
  if (style & WIDGET_STYLE_PRESSED)   // indicate that a button is currently pressed
    sheet += "";
  if (style & WIDGET_STYLE_INERT)  // only render but don't associate any action to widget
    sheet += "";
  if (style & WIDGET_STYLE_BUTTON)  // indicate that a button should explicitly rendered as a button
    sheet += "";
  if (style & WIDGET_STYLE_CENTERED)  // use centered text
    sheet += "text-align: center;";
  if (style & WIDGET_STYLE_BOLD)
    sheet += "font-weight: bold;";
  return sheet;
}

Here is the caller graph for this function:

Definition at line 362 of file qt_utilities.cpp.

                            {
  return  string (QCoreApplication::applicationDirPath () .toAscii() .constData());
  // return from_qstring (QCoreApplication::applicationDirPath ());
}

Here is the caller graph for this function:

void qt_convert_image ( url  image,
url  dest,
int  w,
int  h 
)

Definition at line 297 of file qt_utilities.cpp.

                                                     {
  QImage im (utf8_to_qstring (concretize (image)));
  if (im.isNull ())
    cerr << "TeXmacs] cannot read image file '" << image << "'" 
         << " in qt_convert_image" << LF;
  else {
    if (w > 0 && h > 0) 
      im= im.scaled (w, h, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
    im.scaled (w, h).save (utf8_to_qstring (concretize (dest)));
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

QSize qt_decode_length ( string  width,
QWidget *  qwid 
)

Try to convert a TeXmacs lenght (em, px, w, h) in a QSize.

Uses the widget current size to compute relative sizes as specified with "1w" Should not affect the widget size in that particular case. FIXME: should we use the constant PIXEL somewhere?

Definition at line 110 of file qt_utilities.cpp.

                                               {
  QSize size;
  if (qwid)
    size= qwid->minimumSizeHint();
  else 
    gui_maximal_extents (size.rwidth(), size.rheight());
  
  if (ends (width, "w") && is_double (width (0, N(width) - 1))) {
    double x= as_double (width (0, N(width) - 1));
    size.rwidth() *= x;
  }
  else if (ends (width, "h") && is_double (width (0, N(width) - 1))) {
    double y= as_double (width (0, N(width) - 1));
    size.rheight() *= y;
  }
  else if (ends (width, "em") && is_double (width (0, N(width) - 2))) {
    double x= as_double (width (0, N(width) - 2));
    if (qwid) {
      size.setWidth(x * qwid->fontInfo().pointSize()); 
    } else {
      size.setWidth(x * QApplication::font().pointSize());
    }
  }
  else if (ends (width, "px") && is_double (width (0, N(width) - 2))) {
    double x= as_double (width (0, N(width) - 2));
    size.setWidth(x);
  }
  return size;
}

Here is the call graph for this function:

Here is the caller graph for this function:

string qt_get_date ( string  lan,
string  fm 
)

Definition at line 368 of file qt_utilities.cpp.

                                    {
  QDateTime localtime = QDateTime::currentDateTime();
  if (fm == "") {
    if ((lan == "british") || (lan == "english") || (lan == "american"))
      fm = "MMMM d, yyyy";
    else if (lan == "german")
      fm = "d. MMMM yyyy";
    else if (lan == "chinese" || lan == "japanese" ||
             lan == "korean" || lan == "taiwanese")
    {
      string y = as_string(localtime.date().year());
      string m = as_string(localtime.date().month());
      string d = as_string(localtime.date().day());
      if (lan == "japanese")
        return y * "<#5e74>" * m * "<#6708>" * d * "<#65e5>";
      if (lan == "korean")
        return y * "<#b144> " * m * "<#c6d4> " * d * "<#c77c>";
      return y * "," * m * "," * d;
    }
    else fm = "d MMMM yyyy";
  }
  QLocale loc = QLocale(to_qstring(language_to_locale(lan)));
#if (QT_VERSION >= 0x040400)
  QString date = loc.toString(localtime, to_qstring(fm));
#else
  QString date = localtime.toString(to_qstring(fm));
#endif
  return from_qstring(date);
}

Here is the call graph for this function:

void qt_image_size ( url  image,
int &  w,
int &  h 
)

Definition at line 282 of file qt_utilities.cpp.

                                          {
  //cout <<  concretize (image) << LF;
  QImage im= QImage (utf8_to_qstring (concretize (image)));
  if (im.isNull ()) {
    cerr << "TeXmacs] cannot read image file '" << image << "'" 
        << " in qt_image_size" << LF;
    w= 35; h= 35;
  }
  else {
    w= im.width ();
    h= im.height ();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void qt_image_to_eps ( url  image,
url  eps,
int  w_pt,
int  h_pt,
int  dpi 
)

Definition at line 310 of file qt_utilities.cpp.

                                                                  {
  static const char* d= "0123456789ABCDEF";
  QImage im (utf8_to_qstring (concretize (image)));
  if (im.isNull ())
    cerr << "TeXmacs Cannot read image file '" << image << "'"
        << " in qt_image_to_eps" << LF;
  else {
    if (dpi > 0 && w_pt > 0 && h_pt > 0) {
      int ww= w_pt * dpi / 72;
      int hh= h_pt * dpi / 72;
      if (ww < im.width () || hh < im.height ()) {
        im= im.scaled (ww, hh, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
      }
    }
    string res;
    string sw= as_string (im.width ());
    string sh= as_string (im.height ());
    res << "%!PS-Adobe-3.0 EPSF-3.0\n%%Creator: TeXmacs\n%%BoundingBox: ";
    res << "0 0 " << sw << " " << sh << "\n";
    res << "%%LanguageLevel: 2\n%%Pages: 1\n%%DocumentData: Clean7Bit\n";
    res <<  sw << " " << sh << " scale\n";
    res <<  sw << " " << sh << " 8 [" << sw << " 0 0 -" << sh << " 0 " << sh << "]\n";
    res << "{currentfile 3 " << sw << " mul string readhexstring pop} bind\nfalse 3 colorimage\n";
    int v, i= 0, j= 0, l= 0;
    for (j= 0; j < im.height (); j++) {
      for (i=0; i < im.width (); i++) {
        l++;
        QRgb p= im.pixel (i, j);
        v= qRed (p);
        res << d [(v >> 4)] << d [v % 16];
        v= qGreen (p);
        res << d [(v >> 4)] << d [v % 16];
        v= qBlue (p);
        res << d [(v >> 4)] << d [v % 16];
        if (l > 10) {
          res << "\n";
          l= 0;
        }
      }
    }
    res << "\n%%EOF";
    save_string (eps, res);
#ifdef USE_GS
    url temp= url_temp (".eps");
    gs_to_eps (eps, temp);
    copy (temp, eps);
    remove (temp);
#endif
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static string qt_papersize_to_string ( QPrinter::PaperSize  sz) [static]

Definition at line 402 of file qt_utilities.cpp.

                                               {
  switch (sz) {
      PAPER (A0) ;
      PAPER (A1) ;
      PAPER (A2) ;
      PAPER (A3) ;
      PAPER (A4) ;
      PAPER (A5) ;
      PAPER (A6) ;
      PAPER (A7) ;
      PAPER (A8) ;
      PAPER (A9) ;
      PAPER (B0) ;
      PAPER (B1) ;
      PAPER (B2) ;
      PAPER (B3) ;
      PAPER (B4) ;
      PAPER (B5) ;
      PAPER (B6) ;
      PAPER (B7) ;
      PAPER (B8) ;
      PAPER (B9) ;
      PAPER (B10) ;      
      PAPER (Letter) ;
      
    default:
      return "A4";
  }
}

Here is the caller graph for this function:

bool qt_print ( bool &  to_file,
bool &  landscape,
string pname,
url filename,
string first,
string last,
string paper_type 
)

Definition at line 435 of file qt_utilities.cpp.

                                                           {
  static QPrinter *qprinter = NULL;
  if (!qprinter) {
    qprinter = new QPrinter;
  }
  QPrintDialog pdialog(qprinter);
  if (pdialog.exec() == QDialog::Accepted) {
    to_file = !(qprinter->outputFileName().isNull());
    pname = from_qstring( qprinter->printerName() );
    filename = from_qstring( qprinter->outputFileName() );
    landscape = (qprinter->orientation() == QPrinter::Landscape);
    paper_type = qt_papersize_to_string(qprinter->paperSize());
    if (qprinter->printRange() == QPrinter::PageRange) {
      first = qprinter->fromPage(); 
      last = qprinter->toPage(); 
    }
    //cout << "Printer :" << pname << LF;
    //cout << "File :" << filename << LF;
    return true;
  }
  return false;
}

Here is the call graph for this function:

bool qt_supports ( url  u)

Definition at line 275 of file qt_utilities.cpp.

                    {
  string s= suffix (u);
  if (s == "ps" || s == "eps" || s == "pdf") return false;
  return true;
}

Here is the call graph for this function:

Here is the caller graph for this function:

convert a QString to a TeXmacs cork string

Definition at line 266 of file qt_utilities.cpp.

                        {
  string in_lan= get_input_language ();
  string out_lan= get_output_language ();
//  return tm_var_encode (translate (s, "english", out_lan));
  return tm_var_encode (translate (s, in_lan, out_lan));
}

Here is the call graph for this function:

Here is the caller graph for this function:

QColor to_qcolor ( const string col)

Takes either an hexadecimal RGB color, as in #e3a1ff, or a named color as those defined in src/Graphics/Renderer/rgb_colors.hpp and returns a QColor.

Definition at line 237 of file qt_utilities.cpp.

                              {
  QString _col = to_qstring(col);
  if(_col.startsWith("#"))
    return QColor(_col);
  if(_NamedColors.isEmpty())
    initNamedColors();
  if(_NamedColors.contains(_col))
    return _NamedColors[_col];
  if(DEBUG_QT)
    cout << "to_qcolor(" << col << "): name is not defined in RGBColors.\n";
  return QColor(100,100,100);  // FIXME? 
}

Here is the call graph for this function:

Here is the caller graph for this function:

QColor to_qcolor ( color  c)

Definition at line 258 of file qt_utilities.cpp.

                   {
  int r, g, b, a;
  get_rgb_color (c, r, g, b, a);
  return QColor (r, g, b, a);
}

Here is the call graph for this function:

QPoint to_qpoint ( const coord2 p)

Definition at line 52 of file qt_utilities.cpp.

                             {
  float c= 1.0/PIXEL;
  return QPoint (p.x1*c, -p.x2*c);
}

Here is the caller graph for this function:

QRect to_qrect ( const coord4 p)

Definition at line 46 of file qt_utilities.cpp.

                            {
  float c= 1.0/PIXEL;
  return QRect (p.x1*c, -p.x4*c, (p.x3-p.x1+PIXEL-1)*c, (p.x4-p.x2+PIXEL-1)*c);
}

Here is the caller graph for this function:

QSize to_qsize ( const coord2 p)

Definition at line 58 of file qt_utilities.cpp.

                            {
  float c= 1.0/PIXEL;
  return QSize (p.x1*c, p.x2*c);
}

Here is the caller graph for this function:

QString to_qstring ( string  s)

Definition at line 183 of file qt_utilities.cpp.

                      {
  string out_lan= get_output_language ();
  if ((out_lan == "bulgarian") || 
      (out_lan == "russian") ||
      (out_lan == "ukrainian"))
    s = t2a_to_utf8 (s);
  else
    s = cork_to_utf8 (s);
      
  char* p= as_charp (s);
  QString nss= QString::fromUtf8 (p, N(s));
  tm_delete_array (p);
  return nss;
}

Here is the call graph for this function:

Here is the caller graph for this function:

QStringList to_qstringlist ( array< string l)

Definition at line 167 of file qt_utilities.cpp.

                                {
  QStringList ql;
  for(int i=0; i<N(l); ++i)
    ql << to_qstring(l[i]);
  return ql;
}

Here is the call graph for this function:

Here is the caller graph for this function:

QString to_qstylesheet ( int  style)

Definition at line 88 of file qt_utilities.cpp.

                          {
  return "* {" + parse_tm_style(style) + "}";
}

Here is the call graph for this function:

Here is the caller graph for this function:

QString to_qstylesheet ( int  style,
color  c 
)

Definition at line 93 of file qt_utilities.cpp.

                                   {
  int r,g,b,a;
  get_rgb_color(c, r, g, b, a);
  a = a*100/255;
  return "* {" + parse_tm_style(style)
    + QString("color: rgba(%1, %2, %3, %4%);").arg(r).arg(g).arg(b).arg(a)
    + "}";
}

Here is the call graph for this function:

QString utf8_to_qstring ( string  s)

Definition at line 199 of file qt_utilities.cpp.

                           {
  char* p= as_charp (s);
  QString nss= QString::fromUtf8 (p, N(s));
  tm_delete_array (p);
  return nss;
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

QHash<QString, QColor> _NamedColors [static]

Definition at line 220 of file qt_utilities.cpp.