Commit 30130c5d authored by T.J. Corona's avatar T.J. Corona

Add qtEmittingStringBuffer.

parent a09b6cf5
......@@ -70,6 +70,7 @@ set(QAttrLibMocHeaders
qtBaseView.h
qtCheckItemComboBox.h
qtCollapsibleGroupWidget.h
qtEmittingStringBuffer.h
qtEntityItemDelegate.h
qtEntityItemEditor.h
qtEntityItemModel.h
......@@ -152,3 +153,5 @@ endif()
if (SMTK_ENABLE_EXAMPLES)
add_subdirectory(examples)
endif()
add_subdirectory(testing)
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
// .NAME qtEmittingLogger - a std::stream that emits its output upon flush
// .SECTION Description
// .SECTION See Also
// qtItem
#ifndef __smtk_extension_qtEmittingStringBuffer_h
#define __smtk_extension_qtEmittingStringBuffer_h
#include "smtk/extension/qt/Exports.h"
#include "smtk/io/Logger.h"
#include <QObject>
#include <sstream>
namespace smtk
{
namespace extension
{
/**\brief An std::stringbuf that emits when it is updated.
*
* smtk::io::Logger does not have any access points for derived classes to
* inject actions upon the receipt of a log. So, we set our logger to use a
* std::ostream with a custom std::stringbuf that emits during its sync() call.
*
* NOTE: This is not a very performant way to pass log messages, and could
* become a performance bottleneck.
*/
class qtEmittingStringBuffer : public QObject, public std::stringbuf
{
Q_OBJECT
public:
qtEmittingStringBuffer()
: std::stringbuf()
{
}
virtual ~qtEmittingStringBuffer() { sync(); }
protected:
int sync()
{
emit flush();
str("");
return 0;
}
signals:
void flush();
};
}
}
#endif
#=============================================================================
#
# Copyright (c) Kitware, Inc.
# All rights reserved.
# See LICENSE.txt for details.
#
# This software is distributed WITHOUT ANY WARRANTY; without even
# the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE. See the above copyright notice for more information.
#
#=============================================================================
set(unit_tests_headers
qtPrintLog.h
)
set(unit_tests
UnitTestEmittingStringBuffer.cxx
)
set(unit_tests_which_require_data
)
smtk_qt_wrap_cpp(unit_tests_headers_moc ${unit_tests_headers})
smtk_unit_tests(LABEL "qt"
SOURCES ${unit_tests}
SOURCES_REQUIRE_DATA ${unit_tests_which_require_data}
EXTRA_SOURCES ${unit_tests_headers_moc}
LIBRARIES smtkQtExt smtkCore smtkCoreModelTesting ${Boost_LIBRARIES})
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
// .NAME UnitTestEmittingStringBuffer.h -
// .SECTION Description
// .SECTION See Also
#include "smtk/extension/qt/qtEmittingStringBuffer.h"
#include "smtk/extension/qt/testing/cxx/qtPrintLog.h"
int UnitTestEmittingStringBuffer(int, char** const)
{
// Create an instance of Logger
smtk::io::Logger logger;
// Create an instance of our emitting string buffer
smtk::extension::qtEmittingStringBuffer stringbuf;
// Create a new std::ostream that uses our string buffer
std::ostream* ostr = new std::ostream(&stringbuf);
// Pass the ostream to the logger, and set it to be owned by the logger
logger.setFlushToStream(ostr, true, false);
// Create a PrintLogInstance to connect with our EmittingStringBuffer
qtPrintLog printLog(logger);
QObject::connect(&stringbuf, SIGNAL(flush()), &printLog, SLOT(print()));
// Test the logger.
smtkErrorMacro(logger, "this is an error no = " << 45 << " ERROR!");
smtkWarningMacro(logger, "this is a warning no = " << 10.1234 << " WARNING!");
smtkDebugMacro(logger, "this is a Debug no = " << 1 << " DEBUG!");
logger.addRecord(smtk::io::Logger::INFO, "Sample Info String\n");
return 0;
}
//=========================================================================
// Copyright (c) Kitware, Inc.
// All rights reserved.
// See LICENSE.txt for details.
//
// This software is distributed WITHOUT ANY WARRANTY; without even
// the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE. See the above copyright notice for more information.
//=========================================================================
#ifndef __smtk_extension_qt_testing_cxx_qtPrintLog_h
#define __smtk_extension_qt_testing_cxx_qtPrintLog_h
#include <QObject>
#include "smtk/io/Logger.h"
#include <iostream>
class qtPrintLog : public QObject
{
Q_OBJECT
public:
qtPrintLog(smtk::io::Logger& log)
: Log(log)
{
}
virtual ~qtPrintLog() {}
public slots:
void print()
{
std::cout << "Received: " << Log.convertToString(false) << std::endl;
Log.reset();
}
private:
smtk::io::Logger& Log;
};
#endif
......@@ -116,7 +116,7 @@ public:
, m_ownStream(false)
{
}
~Logger();
virtual ~Logger();
std::size_t numberOfRecords() const { return this->m_records.size(); }
bool hasErrors() const { return this->m_hasErrors; }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment