| 1 |
// Copyright (C) 2014 Gilles Degottex <[email protected]> |
| 2 |
|
| 3 |
// This file is part of "fmit" |
| 4 |
|
| 5 |
// "fmit" is free software; you can redistribute it and/or modify |
| 6 |
// it under the terms of the GNU General Public License as published by |
| 7 |
// the Free Software Foundation; either version 2 of the License, or |
| 8 |
// (at your option) any later version. |
| 9 |
// |
| 10 |
// "fmit" is distributed in the hope that it will be useful, |
| 11 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
// GNU General Public License for more details. |
| 14 |
// |
| 15 |
// You should have received a copy of the GNU General Public License |
| 16 |
// along with this program; if not, write to the Free Software |
| 17 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 |
|
| 19 |
|
| 20 |
#ifndef _QAEHELPERS_H_ |
| 21 |
#define _QAEHELPERS_H_ |
| 22 |
|
| 23 |
#include <QRectF> |
| 24 |
|
| 25 |
#include <iostream> |
| 26 |
|
| 27 |
#include <QThread> |
| 28 |
#include <QDateTime> |
| 29 |
#include <QColor> |
| 30 |
#include <QGraphicsView> |
| 31 |
#include <QTextStream> |
| 32 |
#include <QScrollBar> |
| 33 |
#include <QFileInfo> |
| 34 |
#include <QAudioFormat> |
| 35 |
#include <QDir> |
| 36 |
#include <QDebug> |
| 37 |
|
| 38 |
#ifdef __MINGW32__ |
| 39 |
#define COMPILER "MinGW" |
| 40 |
#elif (defined(__GNUC__) || defined(__GNUG__)) |
| 41 |
#define COMPILER "GCC" |
| 42 |
#define COMPILER_VERSION_MAJOR __GNUC__ |
| 43 |
#define COMPILER_VERSION_MINOR __GNUC_MINOR__ |
| 44 |
#define COMPILER_VERSION_PATCH __GNUC_PATCHLEVEL__ |
| 45 |
#elif defined(_MSC_VER) |
| 46 |
#define COMPILER "MSVC" |
| 47 |
#define COMPILER_VERSION_MAJOR _MSC_VER/100 |
| 48 |
#define COMPILER_VERSION_MINOR _MSC_VER%100 |
| 49 |
#if defined(_MSC_FULL_VER) |
| 50 |
#if _MSC_VER >= 1400 |
| 51 |
/* _MSC_FULL_VER = VVRRPPPPP */ |
| 52 |
#define COMPILER_VERSION_PATCH _MSC_FULL_VER%100000 |
| 53 |
#else |
| 54 |
/* _MSC_FULL_VER = VVRRPPPP */ |
| 55 |
#define COMPILER_VERSION_PATCH _MSC_FULL_VER%10000 |
| 56 |
#endif |
| 57 |
#endif |
| 58 |
#endif |
| 59 |
|
| 60 |
inline QString getCompilerVersion(){ |
| 61 |
QString version; |
| 62 |
|
| 63 |
version += QString(COMPILER); |
| 64 |
#ifdef COMPILER_VERSION_MAJOR |
| 65 |
version += " "+QString::number(COMPILER_VERSION_MAJOR); |
| 66 |
#endif |
| 67 |
#ifdef COMPILER_VERSION_MINOR |
| 68 |
version += "."+QString::number(COMPILER_VERSION_MINOR); |
| 69 |
#endif |
| 70 |
#ifdef COMPILER_VERSION_PATCH |
| 71 |
version += "."+QString::number(COMPILER_VERSION_PATCH); |
| 72 |
#endif |
| 73 |
|
| 74 |
return version; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
// Check if compiling using MSVC (Microsoft compiler) |
| 79 |
#ifdef _MSC_VER |
| 80 |
// The following is necessary for MSVC 2012 |
| 81 |
template<class Type> inline Type log2(Type v) {return std::log(v)/std::log(2.0); } |
| 82 |
#endif |
| 83 |
|
| 84 |
#define QUOTE(name) #name |
| 85 |
#ifdef _MSC_VER |
| 86 |
#define STR(val) QUOTE(val,"") |
| 87 |
#else |
| 88 |
#define STR(val) QUOTE(val) |
| 89 |
#endif |
| 90 |
|
| 91 |
|
| 92 |
#define DCOUT std::cout << QThread::currentThreadId() << " " << QDateTime::fromMSecsSinceEpoch(QDateTime::currentMSecsSinceEpoch()).toString("hh:mm:ss.zzz ").toLocal8Bit().constData() << " " << __FILE__ << ":" << __LINE__ << " " |
| 93 |
|
| 94 |
//#define DCOUT QTextStream(stdout) << QThread::currentThreadId() << " " << QDateTime::fromMSecsSinceEpoch(QDateTime::currentMSecsSinceEpoch()).toString("hh:mm:ss.zzz ").toLocal8Bit().constData() << " " << __FILE__ << ":" << __LINE__ << " " |
| 95 |
|
| 96 |
#define DFLAG DCOUT << std::endl; |
| 97 |
|
| 98 |
// Log file and line automatically |
| 99 |
#define DLOG qDebug() << __FILE__ << ":" << __LINE__ |
| 100 |
|
| 101 |
// Remove hard coded margin (Bug 11945) |
| 102 |
// See: https://bugreports.qt-project.org/browse/QTBUG-11945 |
| 103 |
inline QRectF removeHiddenMargin(QGraphicsView* gv, const QRectF& sceneRect){ |
| 104 |
const int bugMargin = 2; |
| 105 |
const double mx = sceneRect.width()/gv->viewport()->size().width()*bugMargin; |
| 106 |
const double my = sceneRect.height()/gv->viewport()->size().height()*bugMargin; |
| 107 |
return sceneRect.adjusted(mx, my, -mx, -my); |
| 108 |
} |
| 109 |
|
| 110 |
inline QTextStream& operator<<(QTextStream& stream, const QRectF& rectf) { |
| 111 |
stream << "[" << rectf.left() << "," << rectf.right() << "]x[" << rectf.top() << "," << rectf.bottom() << "]"; |
| 112 |
|
| 113 |
return stream; |
| 114 |
} |
| 115 |
|
| 116 |
inline std::ostream& operator<<(std::ostream& stream, const QString& str) { |
| 117 |
stream << str.toLocal8Bit().constData(); |
| 118 |
|
| 119 |
return stream; |
| 120 |
} |
| 121 |
|
| 122 |
inline std::ostream& operator<<(std::ostream& stream, const QRectF& rectf) { |
| 123 |
stream << "[" << rectf.left() << "," << rectf.right() << "]x[" << rectf.top() << "," << rectf.bottom() << "]"; |
| 124 |
|
| 125 |
return stream; |
| 126 |
} |
| 127 |
|
| 128 |
inline std::ostream& operator<<(std::ostream& stream, const QSize& size) { |
| 129 |
stream << size.width() << "x" << size.height(); |
| 130 |
|
| 131 |
return stream; |
| 132 |
} |
| 133 |
|
| 134 |
inline std::ostream& operator<<(std::ostream& stream, const QPoint& p) { |
| 135 |
stream << "(" << p.x() << "," << p.y() << ")"; |
| 136 |
|
| 137 |
return stream; |
| 138 |
} |
| 139 |
|
| 140 |
inline std::ostream& operator<<(std::ostream& stream, const QPointF& p) { |
| 141 |
stream << "(" << p.x() << "," << p.y() << ")"; |
| 142 |
|
| 143 |
return stream; |
| 144 |
} |
| 145 |
|
| 146 |
inline std::ostream& operator<<(std::ostream& stream, const QColor& c) { |
| 147 |
stream << "(" << c.red() << "," << c.green() << "," << c.blue() << ")"; |
| 148 |
|
| 149 |
return stream; |
| 150 |
} |
| 151 |
|
| 152 |
inline std::ostream& operator<<(std::ostream& stream, const QTime& t) { |
| 153 |
stream << t.toString("hh:mm:ss.zzz").toLocal8Bit().constData(); |
| 154 |
|
| 155 |
return stream; |
| 156 |
} |
| 157 |
|
| 158 |
inline QString formatToString(const QAudioFormat &format) { |
| 159 |
|
| 160 |
QString result; |
| 161 |
|
| 162 |
if (QAudioFormat() != format) { |
| 163 |
|
| 164 |
// const QString formatEndian = (format.byteOrder() == QAudioFormat::LittleEndian) |
| 165 |
// ? QString("LE") : QString("BE"); |
| 166 |
const QString formatEndian = QString("Assume LE"); |
| 167 |
|
| 168 |
QString formatType; |
| 169 |
switch (format.sampleFormat()) { |
| 170 |
case QAudioFormat::UInt8: |
| 171 |
formatType = "unsigned"; |
| 172 |
break; |
| 173 |
case QAudioFormat::Int16: |
| 174 |
formatType = "signed"; |
| 175 |
break; |
| 176 |
case QAudioFormat::Int32: |
| 177 |
formatType = "signed"; |
| 178 |
break; |
| 179 |
case QAudioFormat::Float: |
| 180 |
formatType = "float"; |
| 181 |
break; |
| 182 |
case QAudioFormat::Unknown: |
| 183 |
formatType = "unknown"; |
| 184 |
break; |
| 185 |
case QAudioFormat::NSampleFormats: |
| 186 |
formatType = "unknown"; |
| 187 |
break; |
| 188 |
} |
| 189 |
|
| 190 |
QString formatChannels = QString("%1 channels").arg(format.channelCount()); |
| 191 |
switch (format.channelCount()) { |
| 192 |
case 1: |
| 193 |
formatChannels = "mono"; |
| 194 |
break; |
| 195 |
case 2: |
| 196 |
formatChannels = "stereo"; |
| 197 |
break; |
| 198 |
} |
| 199 |
|
| 200 |
// QString codec = format.codec(); |
| 201 |
QString codec = "Assume PCM"; |
| 202 |
|
| 203 |
result = codec+" "+QString("%1Hz %2bit %3 %4 %5") |
| 204 |
.arg(format.sampleRate()) |
| 205 |
.arg(format.bytesPerSample()*8) |
| 206 |
.arg(formatType) |
| 207 |
.arg(formatEndian) |
| 208 |
.arg(formatChannels); |
| 209 |
} |
| 210 |
else { |
| 211 |
result = "empty format"; |
| 212 |
} |
| 213 |
|
| 214 |
return result; |
| 215 |
} |
| 216 |
|
| 217 |
inline std::ostream& operator<<(std::ostream& stream, const QAudioFormat& af) { |
| 218 |
stream << formatToString(af); |
| 219 |
return stream; |
| 220 |
} |
| 221 |
inline QTextStream& operator<<(QTextStream& stream, const QAudioFormat& af) { |
| 222 |
stream << formatToString(af); |
| 223 |
return stream; |
| 224 |
} |
| 225 |
|
| 226 |
// A scrollbar which reset the mouse cursor when hovered |
| 227 |
class QScrollBarHover : public QScrollBar { |
| 228 |
public: |
| 229 |
QScrollBarHover(Qt::Orientation orientation, QWidget * parent) |
| 230 |
: QScrollBar(orientation, parent) { |
| 231 |
setMouseTracking(true); |
| 232 |
} |
| 233 |
|
| 234 |
virtual void mouseMoveEvent(QMouseEvent * e) { |
| 235 |
QScrollBar::mouseMoveEvent(e); |
| 236 |
setCursor(Qt::ArrowCursor); |
| 237 |
} |
| 238 |
}; |
| 239 |
|
| 240 |
inline QString dropFileExtension(const QString& filepath) { |
| 241 |
QFileInfo fileInfo(filepath); |
| 242 |
return fileInfo.path()+QDir::separator()+fileInfo.completeBaseName(); |
| 243 |
} |
| 244 |
|
| 245 |
// COUTD << snd->m_dftparams.nl << " " << snd->m_dftparams.nr << " " << snd->m_dftparams.winlen << " " << snd->m_dftparams.dftlen << " " << snd->m_dftparams.ampscale << " " << snd->m_dftparams.delay << endl; |
| 246 |
|
| 247 |
namespace qae { |
| 248 |
|
| 249 |
inline QString humanReadableSize(float num) { |
| 250 |
static QStringList list; |
| 251 |
if(list.isEmpty()) |
| 252 |
list << "KB" << "MB" << "GB" << "TB"; |
| 253 |
|
| 254 |
QStringListIterator i(list); |
| 255 |
QString unit("bytes"); |
| 256 |
|
| 257 |
while(num >= 1024.0 && i.hasNext()) |
| 258 |
{ |
| 259 |
unit = i.next(); |
| 260 |
num /= 1024.0; |
| 261 |
} |
| 262 |
return QString().setNum(num,'f',2)+""+unit; |
| 263 |
} |
| 264 |
|
| 265 |
}; |
| 266 |
|
| 267 |
#endif // _QAEHELPERS_H_ |