Skip to content

Commit a16113d

Browse files
committed
Reduced memory usage by 2/3. #56
1 parent 50fa8d5 commit a16113d

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

include/ui/ClassroomViewItem.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ class ClassroomViewItem : public QWidget {
2020

2121
private:
2222
QVBoxLayout *verticalLayout;
23-
QPixmap defImage;
2423
QLabel *imageLabel;
2524
QLabel *nameLabel;
2625
};

include/ui/StudentImage.hpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@
66
#define INCLUDE_UI_STUDENTIMAGE_HPP_
77

88
#include <include/ui/Image.hpp>
9+
#include <QImageReader>
910
#include <string>
1011

11-
class StudentImage : public Image {
12+
class StudentImage {
1213
public:
13-
explicit StudentImage(std::string *profileImage) : Image(profileImage, new
14-
std::string("default_user.jpg")) {}
15-
QPixmap setup() override;
14+
explicit StudentImage(const std::string *data_filename);
15+
QImageReader* setup();
16+
17+
private:
18+
QImageReader *imageReader;
19+
20+
void loadImage(const std::string *data_filename,
21+
const std::string *resource_filename);
1622
};
1723

1824
#endif // INCLUDE_UI_STUDENTIMAGE_HPP_

src/ui/ClassroomViewItem.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ void ClassroomViewItem::updateImage() {
4242
auto relImgPath = new std::string(std::string("imgcache/") +
4343
std::to_string(student->getId()) + ".jpg");
4444
auto *studentImage = new StudentImage(relImgPath);
45-
defImage = studentImage->setup();
46-
imageLabel->setPixmap(defImage);
45+
QImage defImage = studentImage->setup()->read();
46+
47+
imageLabel->setPixmap(QPixmap::fromImage(defImage));
4748
}

src/ui/Image.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ void Image::loadImage(const std::string *data_filename,
3030
// Get path to data directory image
3131
path = fm->getDataPath(*data_filename);
3232
} catch (FileNotFound &exception) {
33-
qDebug("User specified image not found; using default");
3433
// Get path to resource directory image
3534
path = fm->getResourcePath(*resource_filename);
3635
}

src/ui/StudentImage.cpp

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,43 @@
33
*/
44

55
#include <include/ui/StudentImage.hpp>
6+
#include <include/FileManager.hpp>
7+
#include <include/FileNotFound.hpp>
8+
#include <QImageReader>
9+
#include <string>
610

7-
QPixmap StudentImage::setup() {
8-
return image.scaled(409, 543, Qt::KeepAspectRatio, Qt::SmoothTransformation);
11+
#define DEFAULT_USER_IMG "default_user.jpg"
12+
13+
StudentImage::StudentImage(const std::string *data_filename) {
14+
loadImage(data_filename, new std::string(DEFAULT_USER_IMG));
15+
}
16+
17+
QImageReader* StudentImage::setup() {
18+
imageReader->setScaledSize(QSize(409, 543));
19+
return imageReader;
20+
}
21+
22+
void StudentImage::loadImage(const std::string *data_filename,
23+
const std::string *resource_filename) {
24+
auto *fm = new FileManager();
25+
26+
// Try to load image from data directory and then try to load from resource
27+
// directory
28+
std::string path;
29+
try {
30+
if (data_filename->empty()) {
31+
throw FileNotFound(new std::string("No image specified"));
32+
}
33+
// Get path to data directory image
34+
path = fm->getDataPath(*data_filename);
35+
} catch (FileNotFound &exception) {
36+
// Get path to resource directory image
37+
path = fm->getResourcePath(*resource_filename);
38+
}
39+
imageReader = new QImageReader(path.c_str());
40+
41+
// Check that image was loaded; this case should never occur but check anyway
42+
if (imageReader->error()) {
43+
qFatal("Image could not be loaded: %s\n", path.c_str());
44+
}
945
}

0 commit comments

Comments
 (0)