From b5246c9694171e5d24b0c91a2a522bdece5acc5d Mon Sep 17 00:00:00 2001 From: Gres Date: Mon, 29 Jun 2015 20:26:33 +0300 Subject: [PATCH] Custom assertion. --- GoogleWebTranslator.cpp | 3 ++- ImageProcessing.cpp | 7 ++++--- Manager.cpp | 7 ++++--- Recognizer.cpp | 5 +++-- ResultDialog.cpp | 11 ++++++----- ScreenTranslator.pro | 3 ++- SelectionDialog.cpp | 7 ++++--- StAssert.h | 15 +++++++++++++++ Translator.cpp | 7 ++++--- 9 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 StAssert.h diff --git a/GoogleWebTranslator.cpp b/GoogleWebTranslator.cpp index 46dccc9..74fc54f 100644 --- a/GoogleWebTranslator.cpp +++ b/GoogleWebTranslator.cpp @@ -7,6 +7,7 @@ #include "GoogleWebTranslator.h" #include "Settings.h" +#include "StAssert.h" GoogleWebTranslator::GoogleWebTranslator() : QObject (), view_ (new QWebView), @@ -82,7 +83,7 @@ void GoogleWebTranslator::replyFinished(QNetworkReply *reply) } void GoogleWebTranslator::load(const ProcessingItem &item) { - Q_ASSERT (!item.recognized.isEmpty ()); + ST_ASSERT (!item.recognized.isEmpty ()); if (translationLanguage_.isEmpty ()) { emit error (tr ("Неверные парметры для перевода.")); return; diff --git a/ImageProcessing.cpp b/ImageProcessing.cpp index 6b55e20..e329674 100644 --- a/ImageProcessing.cpp +++ b/ImageProcessing.cpp @@ -5,6 +5,7 @@ #include #include "ImageProcessing.h" +#include "StAssert.h" #ifdef WIN32 #include @@ -113,10 +114,10 @@ QImage convertImage(Pix &image) Pix *prepareImage(const QImage &image, int preferredScale) { Pix* pix = convertImage (image); - Q_ASSERT (pix != NULL); + ST_ASSERT (pix != NULL); Pix* gray = pixConvertRGBToGray (pix, 0.0, 0.0, 0.0); - Q_ASSERT (gray != NULL); + ST_ASSERT (gray != NULL); pixDestroy (&pix); Pix* scaled = gray; @@ -137,7 +138,7 @@ Pix *prepareImage(const QImage &image, int preferredScale) scaled = pixScale (gray, scale, scale); } - Q_ASSERT (scaled != NULL); + ST_ASSERT (scaled != NULL); if (scaled != gray) { pixDestroy (&gray); diff --git a/Manager.cpp b/Manager.cpp index 4dee7d6..5b713df 100644 --- a/Manager.cpp +++ b/Manager.cpp @@ -18,6 +18,7 @@ #include "Translator.h" #include "ResultDialog.h" #include "LanguageHelper.h" +#include "StAssert.h" Manager::Manager(QObject *parent) : QObject(parent), @@ -134,12 +135,12 @@ Manager::~Manager() void Manager::capture() { QList screens = QApplication::screens (); - Q_ASSERT (!screens.isEmpty ()); + ST_ASSERT (!screens.isEmpty ()); QScreen* screen = screens.first (); Q_CHECK_PTR (screen); WId desktopId = QApplication::desktop ()->winId (); QPixmap pixmap = screen->grabWindow (desktopId); - Q_ASSERT (!pixmap.isNull ()); + ST_ASSERT (!pixmap.isNull ()); emit showPixmap (pixmap); } @@ -203,7 +204,7 @@ void Manager::copyLastToClipboard() void Manager::showResult(ProcessingItem item) { - Q_ASSERT (item.isValid ()); + ST_ASSERT (item.isValid ()); lastItem_ = item; if (useResultDialog_) { diff --git a/Recognizer.cpp b/Recognizer.cpp index c4a1965..b99031a 100644 --- a/Recognizer.cpp +++ b/Recognizer.cpp @@ -7,6 +7,7 @@ #include "Settings.h" #include "ImageProcessing.h" +#include "StAssert.h" Recognizer::Recognizer(QObject *parent) : QObject(parent), @@ -60,7 +61,7 @@ bool Recognizer::initEngine(tesseract::TessBaseAPI *&engine, const QString& lang void Recognizer::recognize(ProcessingItem item) { - Q_ASSERT (!item.source.isNull ()); + ST_ASSERT (!item.source.isNull ()); bool isCustomLanguage = (!item.ocrLanguage.isEmpty () && item.ocrLanguage != ocrLanguage_); tesseract::TessBaseAPI* engine = (isCustomLanguage) ? NULL : engine_; @@ -74,7 +75,7 @@ void Recognizer::recognize(ProcessingItem item) } Pix* image = prepareImage (item.source.toImage (), imageScale_); - Q_ASSERT (image != NULL); + ST_ASSERT (image != NULL); engine->SetImage (image); char* outText = engine->GetUTF8Text(); engine->Clear(); diff --git a/ResultDialog.cpp b/ResultDialog.cpp index efa7602..aeb8ef3 100644 --- a/ResultDialog.cpp +++ b/ResultDialog.cpp @@ -1,5 +1,6 @@ #include "ResultDialog.h" #include "ui_ResultDialog.h" +#include "StAssert.h" #include @@ -32,10 +33,10 @@ bool ResultDialog::eventFilter(QObject* object, QEvent* event) void ResultDialog::showResult(ProcessingItem item) { - Q_ASSERT (!item.source.isNull ()); - Q_ASSERT (!item.recognized.isEmpty ()); - Q_ASSERT (!item.translated.isEmpty ()); - Q_ASSERT (!item.screenPos.isNull ()); + ST_ASSERT (!item.source.isNull ()); + ST_ASSERT (!item.recognized.isEmpty ()); + ST_ASSERT (!item.translated.isEmpty ()); + ST_ASSERT (!item.screenPos.isNull ()); ui->sourceLabel->setPixmap (item.source); ui->recognizeLabel->setText (item.recognized); @@ -61,7 +62,7 @@ void ResultDialog::showResult(ProcessingItem item) { QRect screenRect = desktop->availableGeometry (this); - Q_ASSERT (screenRect.isValid ()); + ST_ASSERT (screenRect.isValid ()); QPoint newPos (screenRect.width () - width (), screenRect.height () - height ()); move (newPos); } diff --git a/ScreenTranslator.pro b/ScreenTranslator.pro index 0d6fa39..8048c03 100644 --- a/ScreenTranslator.pro +++ b/ScreenTranslator.pro @@ -41,7 +41,8 @@ HEADERS += \ ResultDialog.h \ ImageProcessing.h \ LanguageHelper.h \ - GoogleWebTranslator.h + GoogleWebTranslator.h \ + StAssert.h FORMS += \ SettingsEditor.ui \ diff --git a/SelectionDialog.cpp b/SelectionDialog.cpp index 2237f82..ebb77e3 100644 --- a/SelectionDialog.cpp +++ b/SelectionDialog.cpp @@ -1,6 +1,7 @@ #include "SelectionDialog.h" #include "ui_SelectionDialog.h" #include "LanguageHelper.h" +#include "StAssert.h" #include #include @@ -139,9 +140,9 @@ bool SelectionDialog::eventFilter(QObject* object, QEvent* event) return QDialog::eventFilter (object, event); } item.ocrLanguage = dictionary_.ocrUiToCode (action->text ()); - Q_ASSERT (!item.ocrLanguage.isEmpty ()); + ST_ASSERT (!item.ocrLanguage.isEmpty ()); item.sourceLanguage = dictionary_.translateForOcrCode (item.ocrLanguage); - Q_ASSERT (!item.sourceLanguage.isEmpty ()); + ST_ASSERT (!item.sourceLanguage.isEmpty ()); } emit selected (item); accept (); @@ -153,7 +154,7 @@ bool SelectionDialog::eventFilter(QObject* object, QEvent* event) void SelectionDialog::setPixmap(QPixmap pixmap) { - Q_ASSERT (!pixmap.isNull ()); + ST_ASSERT (!pixmap.isNull ()); currentPixmap_ = pixmap; QPalette palette = this->palette (); palette.setBrush (this->backgroundRole (), pixmap); diff --git a/StAssert.h b/StAssert.h new file mode 100644 index 0000000..edeab86 --- /dev/null +++ b/StAssert.h @@ -0,0 +1,15 @@ +#ifndef ST_ASSERT_H +#define ST_ASSERT_H + +#include + +#if !defined(ST_ASSERT) +# if defined(ST_NO_ASSERT) +# define ST_ASSERT(CONDITION) +# else +# define ST_ASSERT(CONDITION) assert(CONDITION) +# endif +#endif + +#endif // ST_ASSERT_H + diff --git a/Translator.cpp b/Translator.cpp index 6196787..f4c8ff0 100644 --- a/Translator.cpp +++ b/Translator.cpp @@ -10,6 +10,7 @@ #include "Settings.h" #include "GoogleWebTranslator.h" +#include "StAssert.h" namespace { @@ -51,7 +52,7 @@ void Translator::translate(ProcessingItem item) emit translateAlternative(item); return; } - Q_ASSERT (!item.recognized.isEmpty ()); + ST_ASSERT (!item.recognized.isEmpty ()); QString sourceLanguage = item.sourceLanguage.isEmpty () ? sourceLanguage_ : item.sourceLanguage; if (translationLanguage_.isEmpty () || sourceLanguage.isEmpty ()) @@ -78,9 +79,9 @@ void Translator::translatedAlternative(ProcessingItem item, bool success) void Translator::replyFinished(QNetworkReply* reply) { - Q_ASSERT (items_.contains (reply)); + ST_ASSERT (items_.contains (reply)); ProcessingItem item = items_.take (reply); - Q_ASSERT (reply->isFinished ()); + ST_ASSERT (reply->isFinished ()); if (reply->error () != QNetworkReply::NoError) { useAlternativeTranslation_ = true;