Naming Convention and clean-up dead code

This commit is contained in:
Sonaion
2019-12-09 19:38:32 +01:00
parent 4c61bae14b
commit 62e144abd4
4 changed files with 171 additions and 189 deletions

View File

@@ -39,7 +39,7 @@ void IntelliPhotoGui::closeEvent(QCloseEvent *event)
// Check if the current image has been changed and then // Check if the current image has been changed and then
// open a dialog to open a file // open a dialog to open a file
void IntelliPhotoGui::open() void IntelliPhotoGui::slotOpen()
{ {
// Check if changes have been made since last save // Check if changes have been made since last save
// maybeSave() returns true if no changes have been made // maybeSave() returns true if no changes have been made
@@ -54,12 +54,12 @@ void IntelliPhotoGui::open()
// If we have a file name load the image and place // If we have a file name load the image and place
// it in the paintingArea // it in the paintingArea
if (!fileName.isEmpty()) if (!fileName.isEmpty())
paintingArea->openImage(fileName); paintingArea->open(fileName);
} }
} }
// Called when the user clicks Save As in the menu // Called when the user clicks Save As in the menu
void IntelliPhotoGui::save() void IntelliPhotoGui::slotSave()
{ {
// A QAction represents the action of the user clicking // A QAction represents the action of the user clicking
QAction *action = qobject_cast<QAction *>(sender()); QAction *action = qobject_cast<QAction *>(sender());
@@ -72,7 +72,7 @@ void IntelliPhotoGui::save()
} }
// Opens a dialog that allows the user to create a New Layer // Opens a dialog that allows the user to create a New Layer
void IntelliPhotoGui::newLayer() void IntelliPhotoGui::slotCreateNewLayer()
{ {
// Stores button value // Stores button value
bool ok1, ok2; bool ok1, ok2;
@@ -90,12 +90,12 @@ void IntelliPhotoGui::newLayer()
if (ok1&&ok2) if (ok1&&ok2)
{ {
int layer = paintingArea->addLayer(width,height,100,100); int layer = paintingArea->addLayer(width,height,100,100);
paintingArea->activate(layer); paintingArea->slotActivateLayer(layer);
} }
} }
// Opens a dialog that allows the user to delete a Layer // Opens a dialog that allows the user to delete a Layer
void IntelliPhotoGui::deleteLayer() void IntelliPhotoGui::slotDeleteLayer()
{ {
// Stores button value // Stores button value
bool ok; bool ok;
@@ -112,7 +112,7 @@ void IntelliPhotoGui::deleteLayer()
} }
void IntelliPhotoGui::onSetAlpha(){ void IntelliPhotoGui::slotSetActiveAlpha(){
// Stores button value // Stores button value
bool ok1, ok2; bool ok1, ok2;
@@ -128,41 +128,41 @@ void IntelliPhotoGui::onSetAlpha(){
255,0, 255, 1, &ok2); 255,0, 255, 1, &ok2);
if (ok1&&ok2) if (ok1&&ok2)
{ {
paintingArea->setAlphaToLayer(layer,alpha); paintingArea->setAlphaOfLayer(layer,alpha);
} }
} }
void IntelliPhotoGui::onMoveUp(){ void IntelliPhotoGui::slotPositionMoveUp(){
paintingArea->moveActive(0,-2); paintingArea->movePositionActive(0,-2);
update(); update();
} }
void IntelliPhotoGui::onMoveDown(){ void IntelliPhotoGui::slotPositionMoveDown(){
paintingArea->moveActive(0,2); paintingArea->movePositionActive(0,2);
update(); update();
} }
void IntelliPhotoGui::onMoveLeft(){ void IntelliPhotoGui::slotPositionMoveLeft(){
paintingArea->moveActive(-2,0); paintingArea->movePositionActive(-2,0);
update(); update();
} }
void IntelliPhotoGui::onMoveRight(){ void IntelliPhotoGui::slotPositionMoveRight(){
paintingArea->moveActive(2,0); paintingArea->movePositionActive(2,0);
update(); update();
} }
void IntelliPhotoGui::onMoveLayerUp(){ void IntelliPhotoGui::slotMoveLayerUp(){
paintingArea->moveActiveLayer(1); paintingArea->moveActiveLayer(1);
update(); update();
} }
void IntelliPhotoGui::onMoveLayerDown(){ void IntelliPhotoGui::slotMoveLayerDown(){
paintingArea->moveActiveLayer(-1); paintingArea->moveActiveLayer(-1);
update(); update();
} }
void IntelliPhotoGui::onClearedPressed(){ void IntelliPhotoGui::slotClearActiveLayer(){
// Stores button value // Stores button value
bool ok1, ok2, ok3, ok4; bool ok1, ok2, ok3, ok4;
@@ -186,11 +186,11 @@ void IntelliPhotoGui::onClearedPressed(){
255,0, 255, 1, &ok4); 255,0, 255, 1, &ok4);
if (ok1&&ok2&&ok3&&ok4) if (ok1&&ok2&&ok3&&ok4)
{ {
paintingArea->clearImage(red, green, blue, alpha); paintingArea->floodFill(red, green, blue, alpha);
} }
} }
void IntelliPhotoGui::onActivePressed(){ void IntelliPhotoGui::slotSetActiveLayer(){
// Stores button value // Stores button value
bool ok1; bool ok1;
@@ -208,7 +208,7 @@ void IntelliPhotoGui::onActivePressed(){
// Open an about dialog // Open an about dialog
void IntelliPhotoGui::about() void IntelliPhotoGui::slotAboutDialog()
{ {
// Window title and text to display // Window title and text to display
QMessageBox::about(this, tr("About Painting"), QMessageBox::about(this, tr("About Painting"),
@@ -219,13 +219,13 @@ void IntelliPhotoGui::about()
void IntelliPhotoGui::createActions() void IntelliPhotoGui::createActions()
{ {
// Create the action tied to the menu // Create the action tied to the menu
openAct = new QAction(tr("&Open..."), this); actionOpen = new QAction(tr("&Open..."), this);
// Define the associated shortcut key // Define the associated shortcut key
openAct->setShortcuts(QKeySequence::Open); actionOpen->setShortcuts(QKeySequence::Open);
// Tie the action to IntelliPhotoGui::open() // Tie the action to IntelliPhotoGui::open()
connect(openAct, SIGNAL(triggered()), this, SLOT(open())); connect(actionOpen, SIGNAL(triggered()), this, SLOT(slotOpen()));
// Get a list of the supported file formats // Get a list of the supported file formats
// QImageWriter is used to write images to files // QImageWriter is used to write images to files
@@ -239,80 +239,80 @@ void IntelliPhotoGui::createActions()
action->setData(format); action->setData(format);
// When clicked call IntelliPhotoGui::save() // When clicked call IntelliPhotoGui::save()
connect(action, SIGNAL(triggered()), this, SLOT(save())); connect(action, SIGNAL(triggered()), this, SLOT(slotSave()));
// Attach each file format option menu item to Save As // Attach each file format option menu item to Save As
saveAsActs.append(action); actionSaveAs.append(action);
} }
//set exporter to actions //set exporter to actions
QAction *pngSaveAction = new QAction("PNG-8", this); QAction *pngSaveAction = new QAction("PNG-8", this);
pngSaveAction->setData("PNG"); pngSaveAction->setData("PNG");
// When clicked call IntelliPhotoGui::save() // When clicked call IntelliPhotoGui::save()
connect(pngSaveAction, SIGNAL(triggered()), this, SLOT(save())); connect(pngSaveAction, SIGNAL(triggered()), this, SLOT(slotSave()));
// Attach each PNG in save Menu // Attach each PNG in save Menu
saveAsActs.append(pngSaveAction); actionSaveAs.append(pngSaveAction);
// Create exit action and tie to IntelliPhotoGui::close() // Create exit action and tie to IntelliPhotoGui::close()
exitAct = new QAction(tr("&Exit"), this); actionOpen = new QAction(tr("&Exit"), this);
exitAct->setShortcuts(QKeySequence::Quit); actionOpen->setShortcuts(QKeySequence::Quit);
connect(exitAct, SIGNAL(triggered()), this, SLOT(close())); connect(actionOpen, SIGNAL(triggered()), this, SLOT(close()));
// Create New Layer action and tie to IntelliPhotoGui::newLayer() // Create New Layer action and tie to IntelliPhotoGui::newLayer()
newLayerAct = new QAction(tr("&New Layer..."), this); actionCreateNewLayer = new QAction(tr("&New Layer..."), this);
connect(newLayerAct, SIGNAL(triggered()), this, SLOT(newLayer())); connect(actionCreateNewLayer, SIGNAL(triggered()), this, SLOT(slotCreateNewLayer()));
// Delete New Layer action and tie to IntelliPhotoGui::deleteLayer() // Delete New Layer action and tie to IntelliPhotoGui::deleteLayer()
deleteLayerAct = new QAction(tr("&Delete Layer..."), this); actionDeleteLayer = new QAction(tr("&Delete Layer..."), this);
connect(deleteLayerAct, SIGNAL(triggered()), this, SLOT(deleteLayer())); connect(actionDeleteLayer, SIGNAL(triggered()), this, SLOT(slotDeleteLayer()));
// Delete Active Layer action and tie to paintingArea::deleteActiveLayerLayer() // Delete Active Layer action and tie to paintingArea::deleteActiveLayerLayer()
deleteActiveLayerAct = new QAction(tr("&Delete active Layer"), this); actionDeleteActiveLayer = new QAction(tr("&Delete active Layer"), this);
connect(deleteActiveLayerAct, SIGNAL(triggered()), paintingArea, SLOT(deleteActiveLayer())); connect(actionDeleteActiveLayer, SIGNAL(triggered()), paintingArea, SLOT(deleteActiveLayer()));
clearedActions = new QAction(tr("&clear Image"), this); actionFloodFill = new QAction(tr("&clear Image"), this);
connect(clearedActions, SIGNAL(triggered()), this, SLOT(onClearedPressed())); connect(actionFloodFill, SIGNAL(triggered()), this, SLOT(slotClearActiveLayer()));
setActiveAction = new QAction(tr("&set Active"), this); actionSetActiveLayer = new QAction(tr("&set Active"), this);
connect(setActiveAction, SIGNAL(triggered()), this, SLOT(onActivePressed())); connect(actionSetActiveLayer, SIGNAL(triggered()), this, SLOT(slotSetActiveLayer()));
setAlphaAction = new QAction(tr("&set Alpha"), this); actionSetActiveAlpha = new QAction(tr("&set Alpha"), this);
connect(setAlphaAction, SIGNAL(triggered()), this, SLOT(onSetAlpha())); connect(actionSetActiveAlpha, SIGNAL(triggered()), this, SLOT(slotSetActiveAlpha()));
moveUpAction = new QAction(tr("&move Up"), this); actionMovePositionUp = new QAction(tr("&move Up"), this);
moveUpAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up)); actionMovePositionUp->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Up));
connect(moveUpAction, SIGNAL(triggered()), this, SLOT(onMoveUp())); connect(actionMovePositionUp, SIGNAL(triggered()), this, SLOT(slotPositionMoveUp()));
moveDownAction = new QAction(tr("&move Down"), this); actionMovePositionDown = new QAction(tr("&move Down"), this);
moveDownAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down)); actionMovePositionDown->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Down));
connect(moveDownAction, SIGNAL(triggered()), this, SLOT(onMoveDown())); connect(actionMovePositionDown, SIGNAL(triggered()), this, SLOT(slotPositionMoveDown()));
moveLeftAction = new QAction(tr("&move Left"), this); actionMovePositionLeft = new QAction(tr("&move Left"), this);
moveLeftAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left)); actionMovePositionLeft->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Left));
connect(moveLeftAction, SIGNAL(triggered()), this, SLOT(onMoveLeft())); connect(actionMovePositionLeft, SIGNAL(triggered()), this, SLOT(slotPositionMoveLeft()));
moveRightAction = new QAction(tr("&move Right"), this); actionMovePositionRight = new QAction(tr("&move Right"), this);
moveRightAction->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right)); actionMovePositionRight->setShortcut(QKeySequence(Qt::CTRL + Qt::Key_Right));
connect(moveRightAction, SIGNAL(triggered()), this, SLOT(onMoveRight())); connect(actionMovePositionRight, SIGNAL(triggered()), this, SLOT(slotPositionMoveRight()));
moveLayerUpAction = new QAction(tr("&move Layer Up"), this); actionMoveLayerUp = new QAction(tr("&move Layer Up"), this);
moveLayerUpAction->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Up)); actionMoveLayerUp->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Up));
connect(moveLayerUpAction, SIGNAL(triggered()), this, SLOT(onMoveLayerUp())); connect(actionMoveLayerUp, SIGNAL(triggered()), this, SLOT(slotMoveLayerUp()));
moveLayerDownAction= new QAction(tr("&move Layer Down"), this); actionMoveLayerDown= new QAction(tr("&move Layer Down"), this);
moveLayerDownAction->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Down)); actionMoveLayerDown->setShortcut(QKeySequence(Qt::CTRL + Qt::ALT + Qt::Key_Down));
connect(moveLayerDownAction, SIGNAL(triggered()), this, SLOT(onMoveLayerDown())); connect(actionMoveLayerDown, SIGNAL(triggered()), this, SLOT(slotMoveLayerDown()));
// Create about action and tie to IntelliPhotoGui::about() // Create about action and tie to IntelliPhotoGui::about()
aboutAct = new QAction(tr("&About"), this); actionAboutDialog = new QAction(tr("&About"), this);
connect(aboutAct, SIGNAL(triggered()), this, SLOT(about())); connect(actionAboutDialog, SIGNAL(triggered()), this, SLOT(slotAboutDialog()));
// Create about Qt action and tie to IntelliPhotoGui::aboutQt() // Create about Qt action and tie to IntelliPhotoGui::aboutQt()
aboutQtAct = new QAction(tr("About &Qt"), this); actionAboutQtDialog = new QAction(tr("About &Qt"), this);
connect(aboutQtAct, SIGNAL(triggered()), qApp, SLOT(aboutQt())); connect(actionAboutQtDialog, SIGNAL(triggered()), qApp, SLOT(aboutQt()));
} }
// Create the menubar // Create the menubar
@@ -320,42 +320,42 @@ void IntelliPhotoGui::createMenus()
{ {
// Create Save As option and the list of file types // Create Save As option and the list of file types
saveAsMenu = new QMenu(tr("&Save As"), this); saveAsMenu = new QMenu(tr("&Save As"), this);
foreach (QAction *action, saveAsActs) foreach (QAction *action, actionSaveAs)
saveAsMenu->addAction(action); saveAsMenu->addAction(action);
// Attach all actions to File // Attach all actions to File
fileMenu = new QMenu(tr("&File"), this); fileMenu = new QMenu(tr("&File"), this);
fileMenu->addAction(openAct); fileMenu->addAction(actionOpen);
fileMenu->addMenu(saveAsMenu); fileMenu->addMenu(saveAsMenu);
fileMenu->addSeparator(); fileMenu->addSeparator();
fileMenu->addAction(exitAct); fileMenu->addAction(actionOpen);
// Attach all actions to Options // Attach all actions to Options
optionMenu = new QMenu(tr("&Options"), this); optionMenu = new QMenu(tr("&Options"), this);
optionMenu->addAction(clearedActions); optionMenu->addAction(actionFloodFill);
optionMenu->addAction(setActiveAction); optionMenu->addAction(actionSetActiveLayer);
optionMenu->addAction(setAlphaAction); optionMenu->addAction(actionSetActiveAlpha);
optionMenu->addAction(moveUpAction); optionMenu->addAction(actionMovePositionUp);
optionMenu->addAction(moveDownAction); optionMenu->addAction(actionMovePositionDown);
optionMenu->addAction(moveLeftAction); optionMenu->addAction(actionMovePositionLeft);
optionMenu->addAction(moveRightAction); optionMenu->addAction(actionMovePositionRight);
optionMenu->addAction(moveLayerUpAction); optionMenu->addAction(actionMoveLayerUp);
optionMenu->addAction(moveLayerDownAction); optionMenu->addAction(actionMoveLayerDown);
// Attach all actions to Layer // Attach all actions to Layer
layerMenu = new QMenu(tr("&Layer"), this); layerMenu = new QMenu(tr("&Layer"), this);
layerMenu->addAction(newLayerAct); layerMenu->addAction(actionCreateNewLayer);
layerMenu->addAction(deleteLayerAct); layerMenu->addAction(actionDeleteLayer);
layerMenu->addAction(deleteActiveLayerAct); layerMenu->addAction(actionDeleteActiveLayer);
//Attach all Tool Options //Attach all Tool Options
toolMenu = new QMenu(tr("&Tools"), this); toolMenu = new QMenu(tr("&Tools"), this);
// Attach all actions to Help // Attach all actions to Help
helpMenu = new QMenu(tr("&Help"), this); helpMenu = new QMenu(tr("&Help"), this);
helpMenu->addAction(aboutAct); helpMenu->addAction(actionAboutDialog);
helpMenu->addAction(aboutQtAct); helpMenu->addAction(actionAboutQtDialog);
// Add menu items to the menubar // Add menu items to the menubar
menuBar()->addMenu(fileMenu); menuBar()->addMenu(fileMenu);
@@ -435,7 +435,7 @@ bool IntelliPhotoGui::saveFile(const QByteArray &fileFormat)
} else { } else {
// Call for the file to be saved // Call for the file to be saved
return paintingArea->saveImage(fileName, fileFormat.constData()); return paintingArea->save(fileName, fileFormat.constData());
} }
} }

View File

@@ -27,21 +27,21 @@ protected:
// The events that can be triggered // The events that can be triggered
private slots: private slots:
void open(); void slotOpen();
void save(); void slotSave();
void newLayer(); void slotCreateNewLayer();
void deleteLayer(); void slotDeleteLayer();
void about(); void slotAboutDialog();
void onClearedPressed(); void slotClearActiveLayer();
void onActivePressed(); void slotSetActiveLayer();
void onSetAlpha(); void slotSetActiveAlpha();
void onMoveUp(); void slotPositionMoveUp();
void onMoveDown(); void slotPositionMoveDown();
void onMoveLeft(); void slotPositionMoveLeft();
void onMoveRight(); void slotPositionMoveRight();
void onMoveLayerUp(); void slotMoveLayerUp();
void onMoveLayerDown(); void slotMoveLayerDown();
private: private:
// Will tie user actions to functions // Will tie user actions to functions
@@ -70,28 +70,28 @@ private:
QMenu *helpMenu; QMenu *helpMenu;
// All the actions that can occur // All the actions that can occur
QAction *openAct; QAction *actionOpen;
QAction *exitAct; QAction *actionExit;
QAction *newLayerAct; QAction *actionCreateNewLayer;
QAction *deleteLayerAct; QAction *actionDeleteLayer;
QAction *deleteActiveLayerAct; QAction *actionDeleteActiveLayer;
QAction *aboutAct; QAction *actionAboutDialog;
QAction *aboutQtAct; QAction *actionAboutQtDialog;
QAction* clearedActions; QAction* actionFloodFill;
QAction* setActiveAction; QAction* actionSetActiveLayer;
QAction* setAlphaAction; QAction* actionSetActiveAlpha;
QAction* moveUpAction; QAction* actionMovePositionUp;
QAction* moveDownAction; QAction* actionMovePositionDown;
QAction* moveLeftAction; QAction* actionMovePositionLeft;
QAction* moveRightAction; QAction* actionMovePositionRight;
QAction* moveLayerUpAction; QAction* actionMoveLayerUp;
QAction* moveLayerDownAction; QAction* actionMoveLayerDown;
// Actions tied to specific file formats // Actions tied to specific file formats
QList<QAction *> saveAsActs; QList<QAction *> actionSaveAs;
//main GUI elements //main GUI elements
QWidget* centralGuiWidget; QWidget* centralGuiWidget;

View File

@@ -16,17 +16,17 @@ PaintingArea::PaintingArea(int maxWidth, int maxHeight, QWidget *parent)
this->setUp(maxWidth, maxHeight); this->setUp(maxWidth, maxHeight);
//tetsing //tetsing
this->addLayer(200,200,0,0,ImageType::Shaped_Image); this->addLayer(200,200,0,0,ImageType::Shaped_Image);
layerStructure[0].image->floodFill(QColor(255,0,0,255)); layerBundle[0].image->floodFill(QColor(255,0,0,255));
std::vector<QPoint> polygon; std::vector<QPoint> polygon;
polygon.push_back(QPoint(100,000)); polygon.push_back(QPoint(100,000));
polygon.push_back(QPoint(200,100)); polygon.push_back(QPoint(200,100));
polygon.push_back(QPoint(100,200)); polygon.push_back(QPoint(100,200));
polygon.push_back(QPoint(000,100)); polygon.push_back(QPoint(000,100));
layerStructure[0].image->setPolygon(polygon); layerBundle[0].image->setPolygon(polygon);
this->addLayer(200,200,150,150); this->addLayer(200,200,150,150);
layerStructure[1].image->floodFill(QColor(0,255,0,255)); layerBundle[1].image->floodFill(QColor(0,255,0,255));
layerStructure[1].alpha=200; layerBundle[1].alpha=200;
activeLayer=1; activeLayer=1;
} }
@@ -40,73 +40,69 @@ void PaintingArea::setUp(int maxWidth, int maxHeight){
// Roots the widget to the top left even if resized // Roots the widget to the top left even if resized
setAttribute(Qt::WA_StaticContents); setAttribute(Qt::WA_StaticContents);
// Set defaults for the monitored variables
scribbling = false;
myPenWidth = 1;
myPenColor = Qt::blue;
} }
int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset, ImageType type){ int PaintingArea::addLayer(int width, int height, int widthOffset, int heightOffset, ImageType type){
LayerObject newLayer; LayerObject newLayer;
newLayer.width = width; newLayer.width = width;
newLayer.height = height; newLayer.hight = height;
newLayer.widthOffset = widthOffset; newLayer.widthOffset = widthOffset;
newLayer.heightOffset = heightOffset; newLayer.hightOffset = heightOffset;
if(type==ImageType::Raster_Image){ if(type==ImageType::Raster_Image){
newLayer.image = new IntelliRasterImage(width,height); newLayer.image = new IntelliRasterImage(width,height);
}else if(type==ImageType::Shaped_Image){ }else if(type==ImageType::Shaped_Image){
newLayer.image = new IntelliShapedImage(width, height); newLayer.image = new IntelliShapedImage(width, height);
} }
newLayer.alpha = 255; newLayer.alpha = 255;
this->layerStructure.push_back(newLayer); this->layerBundle.push_back(newLayer);
return static_cast<int>(layerStructure.size())-1; return static_cast<int>(layerBundle.size())-1;
} }
void PaintingArea::deleteLayer(int index){ void PaintingArea::deleteLayer(int index){
if(index<static_cast<int>(layerStructure.size())){ if(index<static_cast<int>(layerBundle.size())){
this->layerStructure.erase(layerStructure.begin()+index); this->layerBundle.erase(layerBundle.begin()+index);
if(activeLayer>=index){ if(activeLayer>=index){
activeLayer--; activeLayer--;
} }
} }
} }
void PaintingArea::deleteActiveLayer(){ void PaintingArea::slotDeleteActiveLayer(){
if(activeLayer>=0 && activeLayer < static_cast<int>(layerStructure.size())){ if(activeLayer>=0 && activeLayer < static_cast<int>(layerBundle.size())){
this->layerStructure.erase(layerStructure.begin()+activeLayer); this->layerBundle.erase(layerBundle.begin()+activeLayer);
activeLayer--; activeLayer--;
} }
} }
void PaintingArea::setLayerToActive(int index) { void PaintingArea::setLayerToActive(int index) {
if(index>=0&&index<static_cast<int>(layerStructure.size())){ if(index>=0&&index<static_cast<int>(layerBundle.size())){
this->activeLayer=index; this->activeLayer=index;
} }
} }
void PaintingArea::setAlphaToLayer(int index, int alpha){ void PaintingArea::setAlphaOfLayer(int index, int alpha){
if(index>=0&&index<static_cast<int>(layerStructure.size())){ if(index>=0&&index<static_cast<int>(layerBundle.size())){
layerStructure[static_cast<size_t>(index)].alpha=alpha; layerBundle[static_cast<size_t>(index)].alpha=alpha;
} }
} }
// Used to load the image and place it in the widget // Used to load the image and place it in the widget
bool PaintingArea::openImage(const QString &fileName) bool PaintingArea::open(const QString &fileName)
{ {
if(this->activeLayer==-1){ if(this->activeLayer==-1){
return false; return false;
} }
IntelliImage* active = layerStructure[static_cast<size_t>(activeLayer)].image; IntelliImage* active = layerBundle[static_cast<size_t>(activeLayer)].image;
bool open = active->loadImage(fileName); bool open = active->loadImage(fileName);
update(); update();
return open; return open;
} }
// Save the current image // Save the current image
bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat) bool PaintingArea::save(const QString &fileName, const char *fileFormat)
{ {
if(layerStructure.size()==0){ if(layerBundle.size()==0){
return false; return false;
} }
this->assembleLayers(true); this->assembleLayers(true);
@@ -130,30 +126,30 @@ bool PaintingArea::saveImage(const QString &fileName, const char *fileFormat)
// Color the image area with white // Color the image area with white
void PaintingArea::clearImage(int r, int g, int b, int a){ void PaintingArea::floodFill(int r, int g, int b, int a){
if(this->activeLayer==-1){ if(this->activeLayer==-1){
return; return;
} }
IntelliImage* active = layerStructure[static_cast<size_t>(activeLayer)].image; IntelliImage* active = layerBundle[static_cast<size_t>(activeLayer)].image;
active->floodFill(QColor(r, g, b, a)); active->floodFill(QColor(r, g, b, a));
update(); update();
} }
void PaintingArea::moveActive(int x, int y){ void PaintingArea::movePositionActive(int x, int y){
layerStructure[static_cast<size_t>(activeLayer)].widthOffset += x; layerBundle[static_cast<size_t>(activeLayer)].widthOffset += x;
layerStructure[static_cast<size_t>(activeLayer)].heightOffset += y; layerBundle[static_cast<size_t>(activeLayer)].hightOffset += y;
} }
void PaintingArea::moveActiveLayer(int idx){ void PaintingArea::moveActiveLayer(int idx){
if(idx==1){ if(idx==1){
this->activeLayerUp(); this->activateUpperLayer();
}else if(idx==-1){ }else if(idx==-1){
this->activeLayerDown(); this->activateLowerLayer();
} }
} }
void PaintingArea::activate(int a){ void PaintingArea::slotActivateLayer(int a){
if(a>=0 && a < static_cast<int>(layerStructure.size())){ if(a>=0 && a < static_cast<int>(layerBundle.size())){
this->setLayerToActive(a); this->setLayerToActive(a);
} }
} }
@@ -207,16 +203,16 @@ void PaintingArea::resizeImage(QImage *image_res, const QSize &newSize){
//TODO implement //TODO implement
} }
void PaintingArea::activeLayerUp(){ void PaintingArea::activateUpperLayer(){
if(activeLayer!=-1 && activeLayer<layerStructure.size()-1){ if(activeLayer!=-1 && activeLayer<layerBundle.size()-1){
std::swap(layerStructure[activeLayer], layerStructure[activeLayer+1]); std::swap(layerBundle[activeLayer], layerBundle[activeLayer+1]);
activeLayer++; activeLayer++;
} }
} }
void PaintingArea::activeLayerDown(){ void PaintingArea::activateLowerLayer(){
if(activeLayer!=-1 && activeLayer>0){ if(activeLayer!=-1 && activeLayer>0){
std::swap(layerStructure[activeLayer], layerStructure[activeLayer-1]); std::swap(layerBundle[activeLayer], layerBundle[activeLayer-1]);
activeLayer--; activeLayer--;
} }
} }
@@ -227,18 +223,18 @@ void PaintingArea::assembleLayers(bool forSaving){
}else{ }else{
Canvas->fill(Qt::GlobalColor::black); Canvas->fill(Qt::GlobalColor::black);
} }
for(size_t i=0; i<layerStructure.size(); i++){ for(size_t i=0; i<layerBundle.size(); i++){
LayerObject layer = layerStructure[i]; LayerObject layer = layerBundle[i];
QImage cpy = layer.image->getDisplayable(layer.alpha); QImage cpy = layer.image->getDisplayable(layer.alpha);
QColor clr_0; QColor clr_0;
QColor clr_1; QColor clr_1;
for(int y=0; y<layer.height; y++){ for(int y=0; y<layer.hight; y++){
if(layer.heightOffset+y<0) continue; if(layer.hightOffset+y<0) continue;
if(layer.heightOffset+y>=maxHeight) break; if(layer.hightOffset+y>=maxHeight) break;
for(int x=0; x<layer.width; x++){ for(int x=0; x<layer.width; x++){
if(layer.widthOffset+x<0) continue; if(layer.widthOffset+x<0) continue;
if(layer.heightOffset+y>=maxWidth) break; if(layer.hightOffset+y>=maxWidth) break;
clr_0=Canvas->pixelColor(layer.widthOffset+x, layer.heightOffset+y); clr_0=Canvas->pixelColor(layer.widthOffset+x, layer.hightOffset+y);
clr_1=cpy.pixelColor(x,y); clr_1=cpy.pixelColor(x,y);
float t = static_cast<float>(clr_1.alpha())/255.f; float t = static_cast<float>(clr_1.alpha())/255.f;
int r =static_cast<int>(static_cast<float>(clr_1.red())*(t)+static_cast<float>(clr_0.red())*(1.f-t)+0.5f); int r =static_cast<int>(static_cast<float>(clr_1.red())*(t)+static_cast<float>(clr_0.red())*(1.f-t)+0.5f);
@@ -250,7 +246,7 @@ void PaintingArea::assembleLayers(bool forSaving){
clr_0.setBlue(b); clr_0.setBlue(b);
clr_0.setAlpha(a); clr_0.setAlpha(a);
Canvas->setPixelColor(layer.widthOffset+x, layer.heightOffset+y, clr_0); Canvas->setPixelColor(layer.widthOffset+x, layer.hightOffset+y, clr_0);
} }
} }
} }

View File

@@ -12,9 +12,9 @@
struct LayerObject{ struct LayerObject{
IntelliImage* image; IntelliImage* image;
int width; int width;
int height; int hight;
int widthOffset; int widthOffset;
int heightOffset; int hightOffset;
int alpha=255; int alpha=255;
}; };
@@ -29,15 +29,15 @@ public:
PaintingArea(int maxWidth=600, int maxHeight=600, QWidget *parent = nullptr); PaintingArea(int maxWidth=600, int maxHeight=600, QWidget *parent = nullptr);
// Handles all events // Handles all events
bool openImage(const QString &fileName); bool open(const QString &fileName);
bool saveImage(const QString &fileName, const char *fileFormat); bool save(const QString &fileName, const char *fileFormat);
int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type = ImageType::Raster_Image); int addLayer(int width, int height, int widthOffset=0, int heightOffset=0, ImageType type = ImageType::Raster_Image);
void deleteLayer(int index); void deleteLayer(int index);
void setLayerToActive(int index); void setLayerToActive(int index);
void setAlphaToLayer(int index, int alpha); void setAlphaOfLayer(int index, int alpha);
void clearImage(int r, int g, int b, int a); void floodFill(int r, int g, int b, int a);
void moveActive(int x, int y); void movePositionActive(int x, int y);
void moveActiveLayer(int idx); void moveActiveLayer(int idx);
// Has the image been modified since last save // Has the image been modified since last save
@@ -46,11 +46,9 @@ public:
public slots: public slots:
// Events to handle // Events to handle
void activate(int a); void slotActivateLayer(int a);
void deleteActiveLayer(); void slotDeleteActiveLayer();
//void setUp helper for konstruktor
void setUp(int maxWidth, int maxHeight);
protected: protected:
void mousePressEvent(QMouseEvent *event) override; void mousePressEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override; void mouseMoveEvent(QMouseEvent *event) override;
@@ -64,14 +62,15 @@ protected:
void resizeEvent(QResizeEvent *event) override; void resizeEvent(QResizeEvent *event) override;
private: private:
void activeLayerUp(); void setUp(int maxWidth, int maxHeight);
void activeLayerDown(); void activateUpperLayer();
void activateLowerLayer();
QImage* Canvas; QImage* Canvas;
int maxWidth; int maxWidth;
int maxHeight; int maxHeight;
std::vector<LayerObject> layerStructure; std::vector<LayerObject> layerBundle;
int activeLayer=-1; int activeLayer=-1;
void assembleLayers(bool forSaving=false); void assembleLayers(bool forSaving=false);
@@ -81,19 +80,6 @@ private:
// Will be marked true or false depending on if // Will be marked true or false depending on if
// we have saved after a change // we have saved after a change
bool modified=false; bool modified=false;
// Marked true or false depending on if the user
// is drawing
bool scribbling;
// Holds the current pen width & color
int myPenWidth;
QColor myPenColor;
// Stores the image being drawn
// Stores the location at the current mouse event
QPoint lastPoint;
}; };
#endif #endif