Skip to content
Snippets Groups Projects
Commit ba4f4946 authored by Karsten Rink's avatar Karsten Rink
Browse files

Merge pull request #1115 from rinkk/FixColorAndImportLeaks

Fix color and import leaks
parents c2798e6d a002cc96
No related branches found
No related tags found
No related merge requests found
Showing
with 73 additions and 115 deletions
......@@ -23,18 +23,17 @@
namespace GeoLib {
Color* getRandomColor()
Color getRandomColor()
{
std::array<unsigned char, 3> col;
Color col;
col[0] = static_cast<unsigned char>((rand()%5)*50);
col[1] = static_cast<unsigned char>((rand()%5)*50);
col[2] = static_cast<unsigned char>((rand()%5)*50);
return new Color(col);
return col;
}
int readColorLookupTable(std::map<std::string, Color*> &colors, const std::string &filename)
int readColorLookupTable(std::map<std::string, Color> &colors, const std::string &filename)
{
std::ifstream in( filename.c_str() );
if (!in)
......@@ -50,39 +49,32 @@ int readColorLookupTable(std::map<std::string, Color*> &colors, const std::strin
if (fields.size()>=4)
{
Color *c = new Color();
Color c;
id = fields.front();
fields.pop_front();
(*c)[0] = std::atoi(fields.front().c_str());
c[0] = std::atoi(fields.front().c_str());
fields.pop_front();
(*c)[1] = std::atoi(fields.front().c_str());
c[1] = std::atoi(fields.front().c_str());
fields.pop_front();
(*c)[2] = std::atoi(fields.front().c_str());
colors.insert(std::pair<std::string, Color*>(id, c));
c[2] = std::atoi(fields.front().c_str());
colors.insert(std::pair<std::string, Color>(id, c));
}
}
return 1;
}
const Color* getColor(const std::string &id, std::map<std::string, Color*> &colors)
Color const getColor(const std::string &id, std::map<std::string, Color> &colors)
{
for (std::map<std::string, Color*>::const_iterator it=colors.begin(); it !=colors.end(); ++it)
for (std::map<std::string, Color>::const_iterator it=colors.begin(); it !=colors.end(); ++it)
{
if (id.compare(it->first) == 0)
return it->second;
}
WARN("Key \"%s\" not found in color lookup table.", id.c_str());
Color* c = getRandomColor();
colors.insert(std::pair<std::string, Color*>(id, c));
Color c = getRandomColor();
colors.insert(std::pair<std::string, Color>(id, c));
return c;
}
const Color* getColor(double id, std::map<std::string, GeoLib::Color*> &colors)
{
std::ostringstream stream;
stream << id;
return getColor(stream.str(), colors);
}
}
......@@ -25,17 +25,17 @@ namespace GeoLib
using Color = std::array<unsigned char, 3>;
/// Returns a random RGB colour.
Color* getRandomColor();
Color getRandomColor();
/// Reads a color-lookup-table from a file and writes it to a map.
int readColorLookupTable(std::map<std::string, GeoLib::Color*> &colors, const std::string &filename);
int readColorLookupTable(std::map<std::string, GeoLib::Color> &colors, const std::string &filename);
/// Uses a color-lookup-table (in form of a map) to return a colour for a specified name. If the name is not
/// in the colortable a new entry is created with the new name and a random colour.
const Color* getColor(const std::string &id, std::map<std::string, GeoLib::Color*> &colors);
Color const getColor(const std::string &id, std::map<std::string, GeoLib::Color> &colors);
/// Convenience function to use the getColor method with numbers as identifiers.
const Color* getColor(double id, std::map<std::string, GeoLib::Color*> &colors);
Color const getColor(double id, std::map<std::string, GeoLib::Color> &colors);
} // namespace
#endif /* COLOR_H_ */
......@@ -124,9 +124,8 @@ void DetailWindow::resizeWindow()
void DetailWindow::addList(DiagramList* list)
{
GeoLib::Color* c = GeoLib::getRandomColor();
QColor colour((*c)[0], (*c)[1], (*c)[2]);
delete c;
GeoLib::Color const c = GeoLib::getRandomColor();
QColor colour(c[0], c[1], c[2]);
this->addList(list, colour);
resizeWindow();
}
......
......@@ -137,18 +137,11 @@ void StationTreeView::displayStratigraphy()
static_cast<StationTreeModel*>(model())->stationFromIndex(
this->selectionModel()->currentIndex(), temp_name);
// get color table (horrible way to do it but there you go ...)
std::map<std::string,
GeoLib::Color*> colorLookupTable =
static_cast<VtkStationSource*>(static_cast<StationTreeModel*>(model())->vtkSource(
temp_name.
toStdString()))
->getColorLookupTable();
StratWindow* stratView =
new StratWindow(static_cast<GeoLib::StationBorehole*>(static_cast<StationTreeModel*>(
model())
->stationFromIndex(index,
temp_name)),
&colorLookupTable);
std::map<std::string, GeoLib::Color> colorLookupTable =
static_cast<VtkStationSource*>(static_cast<StationTreeModel*>
(model())->vtkSource(temp_name.toStdString()))->getColorLookupTable();
StratWindow* stratView = new StratWindow(static_cast<GeoLib::StationBorehole*>
(static_cast<StationTreeModel*>(model())->stationFromIndex(index,temp_name)), &colorLookupTable);
stratView->setAttribute(Qt::WA_DeleteOnClose); // this fixes the memory leak shown by cppcheck
stratView->show();
}
......@@ -237,38 +230,28 @@ void StationTreeView::showDiagramPrefsDialog()
void StationTreeView::writeStratigraphiesAsImages(QString listName)
{
std::map<std::string,
GeoLib::Color*> colorLookupTable =
static_cast<VtkStationSource*>(static_cast<StationTreeModel*>(model())->vtkSource(
listName.
toStdString()))
->getColorLookupTable();
std::map<std::string, GeoLib::Color> colorLookupTable =
static_cast<VtkStationSource*>(static_cast<StationTreeModel*>
(model())->vtkSource(listName.toStdString()))->getColorLookupTable();
std::vector<ModelTreeItem*> lists = static_cast<StationTreeModel*>(model())->getLists();
std::size_t nLists = lists.size();
for (std::size_t i = 0; i < nLists; i++)
if ( listName.toStdString().compare( lists[i]->data(0).toString().toStdString() )
== 0 )
{
const std::vector<GeoLib::Point*>* stations =
dynamic_cast<BaseItem*>(lists[i]->getItem())->getStations();
{
if ( listName.compare( lists[i]->data(0).toString() ) != 0 )
continue;
std::vector<GeoLib::Point*> const& stations =
*dynamic_cast<BaseItem*>(lists[i]->getItem())->getStations();
for (std::size_t i = 0; i < stations->size(); i++)
{
StratWindow* stratView =
new StratWindow(static_cast<GeoLib::StationBorehole*>((*
stations)
[i]),
&colorLookupTable);
stratView->setAttribute(Qt::WA_DeleteOnClose); // this fixes the memory leak shown by cppcheck
stratView->show();
stratView->stationView->saveAsImage(
"c:/project/" +
QString::fromStdString(static_cast<GeoLib::StationBorehole*>((
*
stations)
[
i])->getName()) + ".jpg");
stratView->close();
}
for (std::size_t i = 0; i < stations.size(); i++)
{
StratWindow* stratView = new StratWindow(
static_cast<GeoLib::StationBorehole*>(stations[i]), &colorLookupTable);
stratView->setAttribute(Qt::WA_DeleteOnClose);
stratView->show();
stratView->stationView->saveAsImage(QString::fromStdString(
static_cast<GeoLib::StationBorehole*>(stations[i])->getName()) + ".jpg");
stratView->close();
}
}
}
......@@ -16,7 +16,7 @@
#include <QPainter>
StratBar::StratBar(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors,
std::map<std::string, GeoLib::Color>* stratColors,
QGraphicsItem* parent) :
QGraphicsItem(parent), _station(station)
{
......@@ -61,11 +61,8 @@ void StratBar::paint(QPainter* painter, const QStyleOptionGraphicsItem* option,
top += height;
height = logHeight(((*(profile[i - 1]))[2] - (*(profile[i]))[2]));
QRectF layer(0, top, BARWIDTH, height);
const GeoLib::Color* c (GeoLib::getColor(soilNames[i], _stratColors));
QBrush brush(QColor((int)(*c)[0],
(int)(*c)[1],
(int)(*c)[2],
127), Qt::SolidPattern);
GeoLib::Color const& c (GeoLib::getColor(soilNames[i], _stratColors));
QBrush brush(QColor((int)c[0], (int)c[1], (int)c[2], 127), Qt::SolidPattern);
painter->setBrush(brush);
painter->drawRect(layer);
......
......@@ -36,7 +36,7 @@ public:
* \param parent The parent QGraphicsItem.
*/
StratBar(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors = NULL,
std::map<std::string, GeoLib::Color>* stratColors = nullptr,
QGraphicsItem* parent = 0);
~StratBar();
......@@ -60,7 +60,7 @@ private:
static const int BARWIDTH = 50;
GeoLib::StationBorehole* _station;
std::map<std::string, GeoLib::Color*> _stratColors;
std::map<std::string, GeoLib::Color> _stratColors;
};
#endif //STRATBAR_H
......@@ -23,7 +23,7 @@
#include "StratScene.h"
StratScene::StratScene(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors,
std::map<std::string, GeoLib::Color>* stratColors,
QObject* parent) : QGraphicsScene(parent)
{
QRectF textBounds;
......@@ -115,7 +115,7 @@ void StratScene::addSoilNameLabels(std::vector<std::string> soilNames,
}
StratBar* StratScene::addStratBar(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors)
std::map<std::string, GeoLib::Color>* stratColors)
{
StratBar* b = new StratBar(station, stratColors);
addItem(b);
......
......@@ -30,7 +30,7 @@ class StratScene : public QGraphicsScene
public:
/// Constructor
StratScene(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors = NULL,
std::map<std::string, GeoLib::Color>* stratColors = nullptr,
QObject* parent = 0);
~StratScene();
......@@ -52,7 +52,7 @@ private:
/// Add a stratigraphy-bar to the scene.
StratBar* addStratBar(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors = NULL);
std::map<std::string, GeoLib::Color>* stratColors = nullptr);
};
#endif //STRATSCENE_H
......@@ -22,7 +22,7 @@ StratView::~StratView()
}
void StratView::setStation(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors)
std::map<std::string, GeoLib::Color>* stratColors)
{
_scene = new StratScene(station, stratColors);
setScene(_scene);
......
......@@ -39,7 +39,7 @@ public:
/// Sets the Borehole whose data should be visualised.
void setStation(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors = NULL);
std::map<std::string, GeoLib::Color>* stratColors = nullptr);
/// Returns the height of the bounding rectangle of all objects within the scene.
int getHeight() { return static_cast<int>((_scene->itemsBoundingRect()).height()); }
......
......@@ -16,7 +16,7 @@
#include "StratWindow.h"
StratWindow::StratWindow(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors,
std::map<std::string, GeoLib::Color>* stratColors,
QWidget* parent) : QWidget(parent)
{
setupUi(this);
......
......@@ -38,7 +38,7 @@ public:
* \param parent The parent QWidget.
*/
StratWindow(GeoLib::StationBorehole* station,
std::map<std::string, GeoLib::Color*>* stratColors = NULL,
std::map<std::string, GeoLib::Color>* stratColors = nullptr,
QWidget* parent = 0);
~StratWindow(void) { this->destroy(); }
......
......@@ -41,9 +41,8 @@ VtkPointsSource::VtkPointsSource()
_removable = false; // From VtkAlgorithmProperties
this->SetNumberOfInputPorts(0);
const GeoLib::Color* c = GeoLib::getRandomColor();
GetProperties()->SetColor((*c)[0] / 255.0,(*c)[1] / 255.0,(*c)[2] / 255.0);
delete c;
const GeoLib::Color c = GeoLib::getRandomColor();
GetProperties()->SetColor(c[0] / 255.0,c[1] / 255.0,c[2] / 255.0);
}
void VtkPointsSource::PrintSelf( ostream& os, vtkIndent indent )
......
......@@ -42,9 +42,8 @@ VtkPolylinesSource::VtkPolylinesSource()
_removable = false; // From VtkAlgorithmProperties
this->SetNumberOfInputPorts(0);
const GeoLib::Color* c = GeoLib::getRandomColor();
GetProperties()->SetColor((*c)[0] / 255.0,(*c)[1] / 255.0,(*c)[2] / 255.0);
delete c;
const GeoLib::Color c = GeoLib::getRandomColor();
GetProperties()->SetColor(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
}
VtkPolylinesSource::~VtkPolylinesSource()
......
......@@ -42,16 +42,8 @@ VtkStationSource::VtkStationSource()
_removable = false; // From VtkAlgorithmProperties
this->SetNumberOfInputPorts(0);
const GeoLib::Color* c = GeoLib::getRandomColor();
GetProperties()->SetColor((*c)[0] / 255.0,(*c)[1] / 255.0,(*c)[2] / 255.0);
delete c;
}
VtkStationSource::~VtkStationSource()
{
std::map<std::string, GeoLib::Color*>::iterator it;
for (it = _colorLookupTable.begin(); it != _colorLookupTable.end(); ++it)
delete it->second;
const GeoLib::Color c = GeoLib::getRandomColor();
GetProperties()->SetColor(c[0] / 255.0, c[1] / 255.0, c[2] / 255.0);
}
void VtkStationSource::PrintSelf( ostream& os, vtkIndent indent )
......
......@@ -36,7 +36,7 @@ public:
/// Returns the colour lookup table generated for boreholes.
/// This method should only be called after the colour lookup table has actually been build (via RequestData() or setColorLookupTable()).
const std::map<std::string, GeoLib::Color*>& getColorLookupTable() const
const std::map<std::string, GeoLib::Color>& getColorLookupTable() const
{ return _colorLookupTable; }
/// Returns the type of observation site represented in this source object
......@@ -57,7 +57,6 @@ public:
protected:
VtkStationSource();
~VtkStationSource();
/// Computes the polygonal data object.
int RequestData(vtkInformation* request,
......@@ -73,7 +72,7 @@ protected:
/// The colour table for stratigraphic data. This table is either set using the setColorLookupTable() method or is generated
/// automatically with random colours while creating the VtkStationSource-object.
std::map<std::string, GeoLib::Color*> _colorLookupTable;
std::map<std::string, GeoLib::Color> _colorLookupTable;
private:
std::size_t GetIndexByName( std::string const& name );
......
......@@ -39,9 +39,9 @@ VtkSurfacesSource::VtkSurfacesSource()
this->SetNumberOfInputPorts(0);
//this->SetColorBySurface(true);
const GeoLib::Color* c = GeoLib::getRandomColor();
const GeoLib::Color c = GeoLib::getRandomColor();
vtkProperty* vtkProps = GetProperties();
vtkProps->SetColor((*c)[0] / 255.0,(*c)[1] / 255.0,(*c)[2] / 255.0);
vtkProps->SetColor(c[0] / 255.0,c[1] / 255.0,c[2] / 255.0);
vtkProps->SetEdgeVisibility(0);
}
......
......@@ -176,17 +176,17 @@ void VtkVisPipeline::loadFromFile(QString filename)
if (filename.size() > 0)
{
vtkXMLDataReader* reader;
vtkSmartPointer<vtkXMLDataReader> reader;
if (filename.endsWith("vti"))
reader = vtkXMLImageDataReader::New();
reader = vtkSmartPointer<vtkXMLImageDataReader>::New();
else if (filename.endsWith("vtr"))
reader = vtkXMLRectilinearGridReader::New();
reader = vtkSmartPointer<vtkXMLRectilinearGridReader>::New();
else if (filename.endsWith("vts"))
reader = vtkXMLStructuredGridReader::New();
reader = vtkSmartPointer<vtkXMLStructuredGridReader>::New();
else if (filename.endsWith("vtp"))
reader = vtkXMLPolyDataReader::New();
reader = vtkSmartPointer<vtkXMLPolyDataReader>::New();
else if (filename.endsWith("vtu"))
reader = vtkXMLUnstructuredGridReader::New();
reader = vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
else if (filename.endsWith("vtk"))
{
vtkGenericDataObjectReader* oldStyleReader =
......@@ -214,7 +214,7 @@ void VtkVisPipeline::loadFromFile(QString filename)
//std::cout << "#cell scalars: " << reader->GetNumberOfCellArrays() << std::endl;
//std::cout << "#point scalars: " << reader->GetNumberOfPointArrays() << std::endl;
vtkDataSet* dataSet = reader->GetOutputAsDataSet();
vtkSmartPointer<vtkDataSet> dataSet = reader->GetOutputAsDataSet();
if (dataSet)
{
this->listArrays(dataSet);
......@@ -222,8 +222,6 @@ void VtkVisPipeline::loadFromFile(QString filename)
}
else
ERR("VtkVisPipeline::loadFromFile(): not a valid vtkDataSet.");
//reader->Delete();
}
#ifndef NDEBUG
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment