Qt 4.8
qcoloroutput.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtXmlPatterns module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include <QFile>
43 #include <QHash>
44 #include <QTextCodec>
45 
46 #include "qcoloroutput_p.h"
47 
48 // TODO: rename insertMapping() to insertColorMapping()
49 // TODO: Use a smart pointer for managing ColorOutputPrivate *d;
50 // TODO: break out the C++ example into a snippet file
51 
52 /* This include must appear here, because if it appears at the beginning of the file for
53  * instance, it breaks build -- "qglobal.h:628: error: template with
54  * C linkage" -- on Mac OS X 10.4. */
55 #ifndef Q_OS_WIN
56 #include <unistd.h>
57 #endif
58 
60 
61 using namespace QPatternist;
62 
63 namespace QPatternist
64 {
66  {
67  public:
69 
70  {
71  /* - QIODevice::Unbuffered because we want it to appear when the user actually calls, performance
72  * is considered of lower priority.
73  */
75 
77  }
78 
82 
83  static const char *const foregrounds[];
84  static const char *const backgrounds[];
85 
86  inline void write(const QString &msg)
87  {
88  m_out.write(msg.toLocal8Bit());
89  }
90 
91  static QString escapeCode(const QString &in)
92  {
93  QString result;
94  result.append(QChar(0x1B));
95  result.append(QLatin1Char('['));
96  result.append(in);
97  result.append(QLatin1Char('m'));
98  return result;
99  }
100 
101  private:
103 
107  inline bool isColoringPossible() const
108  {
109 # if defined(Q_OS_WIN32) || defined(Q_OS_WINCE)
110  /* Windows doesn't at all support ANSI escape codes, unless
111  * the user install a "device driver". See the Wikipedia links in the
112  * class documentation for details. */
113  return false;
114 # else
115  /* We use QFile::handle() to get the file descriptor. It's a bit unsure
116  * whether it's 2 on all platforms and in all cases, so hopefully this layer
117  * of abstraction helps handle such cases. */
118  return isatty(m_out.handle());
119 # endif
120  }
121  };
122 }
123 
124 const char *const ColorOutputPrivate::foregrounds[] =
125 {
126  "0;30",
127  "0;34",
128  "0;32",
129  "0;36",
130  "0;31",
131  "0;35",
132  "0;33",
133  "0;37",
134  "1;30",
135  "1;34",
136  "1;32",
137  "1;36",
138  "1;31",
139  "1;35",
140  "1;33",
141  "1;37"
142 };
143 
144 const char *const ColorOutputPrivate::backgrounds[] =
145 {
146  "0;40",
147  "0;44",
148  "0;42",
149  "0;46",
150  "0;41",
151  "0;45",
152  "0;43"
153 };
154 
254 {
255  d->colorMapping = cMapping;
256 }
257 
264 {
265  return d->colorMapping;
266 }
267 
272 {
273 }
274 
279 {
280  delete d;
281 }
282 
295 void ColorOutput::write(const QString &message, int colorID)
296 {
297  d->write(colorify(message, colorID));
298 }
299 
307 {
308  d->write(message + QLatin1Char('\n'));
309 }
310 
319 QString ColorOutput::colorify(const QString &message, int colorID) const
320 {
321  Q_ASSERT_X(colorID == -1 || d->colorMapping.contains(colorID), Q_FUNC_INFO,
322  qPrintable(QString::fromLatin1("There is no color registered by id %1").arg(colorID)));
323  Q_ASSERT_X(!message.isEmpty(), Q_FUNC_INFO, "It makes no sense to attempt to print an empty string.");
324 
325  if(colorID != -1)
326  d->currentColorID = colorID;
327 
328  if(d->coloringEnabled && colorID != -1)
329  {
330  const int color(d->colorMapping.value(colorID));
331 
332  /* If DefaultColor is set, we don't want to color it. */
333  if(color & DefaultColor)
334  return message;
335 
336  const int foregroundCode = (int(color) & ForegroundMask) >> ForegroundShift;
337  const int backgroundCode = (int(color) & BackgroundMask) >> BackgroundShift;
338  QString finalMessage;
339  bool closureNeeded = false;
340 
341  if(foregroundCode)
342  {
344  closureNeeded = true;
345  }
346 
347  if(backgroundCode)
348  {
350  closureNeeded = true;
351  }
352 
353  finalMessage.append(message);
354 
355  if(closureNeeded)
356  {
357  finalMessage.append(QChar(0x1B));
358  finalMessage.append(QLatin1String("[0m"));
359  }
360 
361  return finalMessage;
362  }
363  else
364  return message;
365 }
366 
375 void ColorOutput::insertMapping(int colorID, const ColorCode colorCode)
376 {
377  d->colorMapping.insert(colorID, colorCode);
378 }
379 
double d
Definition: qnumeric_p.h:62
static const char *const foregrounds[]
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static const char *const backgrounds[]
bool open(OpenMode flags)
Opens the file using OpenMode mode, returning true if successful; otherwise false.
Definition: qfile.cpp:1064
void writeUncolored(const QString &message)
Writes message to stderr as if for instance QTextStream would have been used, and adds a line ending ...
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QString class provides a Unicode character string.
Definition: qstring.h:83
bool contains(const Key &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
The QChar class provides a 16-bit Unicode character.
Definition: qchar.h:72
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
QString colorify(const QString &message, int color=-1) const
Treats message and colorID identically to write(), but instead of writing message to stderr...
void write(const QString &msg)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void setColorMapping(const ColorMapping &cMapping)
Sets the color mapping to be cMapping.
static QString escapeCode(const QString &in)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
The namespace for the internal API of QtXmlPatterns.
ColorOutput()
Constructs a ColorOutput instance, ready for use.
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
int handle() const
Returns the file handle of the file.
Definition: qfile.cpp:1419
ColorOutputPrivate * d
void insertMapping(int colorID, const ColorCode colorCode)
Adds a color mapping from colorID to colorCode, for this ColorOutput instance.
#define Q_ASSERT_X(cond, where, what)
Definition: qglobal.h:1837
bool isColoringPossible() const
Returns true if it&#39;s suitable to send colored output to stderr.
QString & append(QChar c)
Definition: qstring.cpp:1777
The QFile class provides an interface for reading from and writing to files.
Definition: qfile.h:65
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
~ColorOutput()
Destructs this ColorOutput instance.
ColorMapping colorMapping() const
Returns the color mappings in use.
The QFlags class provides a type-safe way of storing OR-combinations of enum values.
Definition: qglobal.h:2313
ColorOutput::ColorMapping colorMapping
qint64 write(const char *data, qint64 len)
Writes at most maxSize bytes of data from data to the device.
Definition: qiodevice.cpp:1342
#define qPrintable(string)
Definition: qglobal.h:1750
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
void write(const QString &message, int color=-1)
Sends message to stderr, using the color looked up in colorMapping() using colorID.
#define Q_FUNC_INFO
Definition: qglobal.h:1871