Skip to content

Commit a87b530

Browse files
authored
Merge pull request #11 from XuhuaHuang/develop
Develop
2 parents 61254b8 + 7e7da8b commit a87b530

File tree

11 files changed

+478
-0
lines changed

11 files changed

+478
-0
lines changed

Array/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(Array LANGUAGES C)
4+
5+
set(CMAKE_C_STANDARD 11)
6+
set(CMAKE_C_STANDARD_REQUIRED ON)
7+
8+
# Adding multiple executable to the project
9+
# Right-click on each item to set as start-up project
10+
add_executable(ArrayCStyle "array.c")
11+
add_executable(MatrixCStyle "matrix.c")

Array/array.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*****************************************************************//**
2+
* \file array.c
3+
* \brief gcc -c array.c -o array.exe -Wall
4+
*
5+
* \author Xuhua Huang
6+
* \date November 2022
7+
*********************************************************************/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
void displayArray(int arr[], const int size) {
13+
for (int i = 0; i < size; i++) {
14+
printf("%d, ", arr[i]);
15+
}
16+
printf("\n");
17+
return;
18+
}
19+
20+
// the displayArray function defined above
21+
// is the same as the one defined below
22+
// int arr[] will decade to int* arr
23+
// void displayArray(int* arr, const int size) {
24+
// for (int i = 0; i < size; i++) {
25+
// printf("%d, ", *(arr+i));
26+
// }
27+
// printf("\n");
28+
// return;
29+
// }
30+
31+
int main(void) {
32+
int vector[5] = { 1, 2, 3, 4, 5 };
33+
displayArray(vector, 5);
34+
35+
printf("With dynamic array size determination:\n");
36+
displayArray(vector, sizeof(vector)/sizeof(vector[0]));
37+
38+
system("pause");
39+
return EXIT_SUCCESS;
40+
}

Array/matrix.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*****************************************************************//**
2+
* \file matrix.c
3+
* \brief
4+
*
5+
* \author Xuhua Huang
6+
* \date November 2022
7+
*********************************************************************/
8+
9+
#include <stdio.h>
10+
#include <stdlib.h>
11+
12+
int main(void) {
13+
int matrix[2][5] = {
14+
{1, 2, 3, 4, 5},
15+
{6, 7, 8, 9, 10}
16+
};
17+
for (int row = 0; row < 2; row++) {
18+
for (int col = 0; col < 5; col++) {
19+
printf("matrix[%d][%d] Address: %p Value: %d\n",
20+
row, col,
21+
&matrix[row][col],
22+
matrix[row][col]);
23+
}
24+
}
25+
return EXIT_SUCCESS;
26+
}

Inheritance/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
cmake_minimum_required(VERSION 3.20)
2+
3+
project(Inheritance)
4+
5+
set(CMAKE_CXX_STANDARD 11)
6+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
7+
8+
# Adding multiple executable to the project
9+
# Right-click on each item to set as start-up project
10+
add_executable(InheritanceNotes "Inheritance_notes.cpp")
11+
12+
add_subdirectory(BankingAccounts)
13+
add_subdirectory(Relatives)

Qt/ImageViewer/ImageViewer.pro

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
######################################################################
2+
# Automatically generated by qmake (3.1) Sun Nov 27 14:40:35 2022
3+
######################################################################
4+
5+
TEMPLATE = app
6+
TARGET = ImageViewer
7+
8+
QT += core gui network
9+
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
10+
11+
INCLUDEPATH += .
12+
13+
# The following define makes your compiler warn you if you use any
14+
# feature of Qt which has been marked as deprecated (the exact warnings
15+
# depend on your compiler). Please consult the documentation of the
16+
# deprecated API in order to know how to port your code away from it.
17+
DEFINES += QT_DEPRECATED_WARNINGS
18+
19+
# You can also make your code fail to compile if you use deprecated APIs.
20+
# In order to do so, uncomment the following line.
21+
# You can also select to disable deprecated APIs only up to a certain version of Qt.
22+
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
23+
24+
# Input
25+
HEADERS += mainwindow.h
26+
SOURCES += main.cpp mainwindow.cpp

Qt/ImageViewer/main.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*****************************************************************//**
2+
* \file main.cpp
3+
* \brief Use command `qmake -project` to generate the Qt project file
4+
* The following lines are required in the ImageViewer.pro file:
5+
* QT += core gui network
6+
* greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
7+
*
8+
* \author Xuhua Huang
9+
* \date November 27, 2022
10+
*********************************************************************/
11+
12+
#include <QApplication>
13+
#include <QMainWindow>
14+
#include "mainwindow.h"
15+
16+
int main(int argc, char** argv) {
17+
QApplication app(argc, argv);
18+
MainWindow window;
19+
window.setWindowTitle("Image Viewer");
20+
window.show();
21+
22+
return app.exec();
23+
}

Qt/ImageViewer/mainwindow.cpp

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
/*****************************************************************//**
2+
* \file mainwindow.cpp
3+
* \brief
4+
*
5+
* \author Xuhua Huang
6+
* \date November 2022
7+
*********************************************************************/
8+
9+
#include <QApplication>
10+
#include <QFileDialog>
11+
#include <QMessageBox>
12+
#include <QPixmap>
13+
#include <QKeyEvent>
14+
#include <QDebug>
15+
#include "mainwindow.h"
16+
17+
MainWindow::MainWindow(QWidget* parent) :
18+
QMainWindow(parent)
19+
, fileMenu(nullptr)
20+
, viewMenu(nullptr)
21+
, currentImage(nullptr)
22+
{
23+
initUI();
24+
}
25+
26+
MainWindow::~MainWindow()
27+
{
28+
}
29+
30+
void MainWindow::initUI()
31+
{
32+
this->resize(800, 600);
33+
// setup menubar
34+
fileMenu = menuBar()->addMenu("&File");
35+
viewMenu = menuBar()->addMenu("&View");
36+
37+
// setup toolbar
38+
fileToolBar = addToolBar("File");
39+
viewToolBar = addToolBar("View");
40+
41+
// main area for image display
42+
imageScene = new QGraphicsScene(this);
43+
imageView = new QGraphicsView(imageScene);
44+
setCentralWidget(imageView);
45+
46+
// setup status bar
47+
mainStatusBar = statusBar();
48+
mainStatusLabel = new QLabel(mainStatusBar);
49+
mainStatusBar->addPermanentWidget(mainStatusLabel);
50+
mainStatusLabel->setText("Image Information will be here!");
51+
52+
createActions();
53+
}
54+
55+
void MainWindow::createActions()
56+
{
57+
// create actions, add them to menus
58+
openAction = new QAction("&Open", this);
59+
fileMenu->addAction(openAction);
60+
saveAsAction = new QAction("&Save as", this);
61+
fileMenu->addAction(saveAsAction);
62+
exitAction = new QAction("E&xit", this);
63+
fileMenu->addAction(exitAction);
64+
65+
zoomInAction = new QAction("Zoom in", this);
66+
viewMenu->addAction(zoomInAction);
67+
zoomOutAction = new QAction("Zoom Out", this);
68+
viewMenu->addAction(zoomOutAction);
69+
prevAction = new QAction("&Previous Image", this);
70+
viewMenu->addAction(prevAction);
71+
nextAction = new QAction("&Next Image", this);
72+
viewMenu->addAction(nextAction);
73+
74+
// add actions to toolbars
75+
fileToolBar->addAction(openAction);
76+
viewToolBar->addAction(zoomInAction);
77+
viewToolBar->addAction(zoomOutAction);
78+
viewToolBar->addAction(prevAction);
79+
viewToolBar->addAction(nextAction);
80+
81+
// connect signals and slots
82+
connect(exitAction, SIGNAL(triggered(bool)), QApplication::instance(), SLOT(quit()));
83+
connect(openAction, SIGNAL(triggered(bool)), this, SLOT(openImage()));
84+
connect(saveAsAction, SIGNAL(triggered(bool)), this, SLOT(saveAs()));
85+
connect(zoomInAction, SIGNAL(triggered(bool)), this, SLOT(zoomIn()));
86+
connect(zoomOutAction, SIGNAL(triggered(bool)), this, SLOT(zoomOut()));
87+
connect(prevAction, SIGNAL(triggered(bool)), this, SLOT(prevImage()));
88+
connect(nextAction, SIGNAL(triggered(bool)), this, SLOT(nextImage()));
89+
90+
setupShortcuts();
91+
}
92+
93+
void MainWindow::openImage()
94+
{
95+
QFileDialog dialog(this);
96+
dialog.setWindowTitle("Open Image");
97+
dialog.setFileMode(QFileDialog::ExistingFile);
98+
dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)"));
99+
QStringList filePaths;
100+
if (dialog.exec()) {
101+
filePaths = dialog.selectedFiles();
102+
showImage(filePaths.at(0));
103+
}
104+
}
105+
106+
107+
void MainWindow::showImage(QString path)
108+
{
109+
imageScene->clear();
110+
imageView->resetMatrix();
111+
QPixmap image(path);
112+
currentImage = imageScene->addPixmap(image);
113+
imageScene->update();
114+
imageView->setSceneRect(image.rect());
115+
QString status = QString("%1, %2x%3, %4 Bytes").arg(path).arg(image.width())
116+
.arg(image.height()).arg(QFile(path).size());
117+
mainStatusLabel->setText(status);
118+
currentImagePath = path;
119+
}
120+
121+
void MainWindow::zoomIn()
122+
{
123+
imageView->scale(1.2, 1.2);
124+
}
125+
126+
void MainWindow::zoomOut()
127+
{
128+
imageView->scale(1 / 1.2, 1 / 1.2);
129+
}
130+
131+
void MainWindow::prevImage()
132+
{
133+
QFileInfo current(currentImagePath);
134+
QDir dir = current.absoluteDir();
135+
QStringList nameFilters;
136+
nameFilters << "*.png" << "*.bmp" << "*.jpg";
137+
QStringList fileNames = dir.entryList(nameFilters, QDir::Files, QDir::Name);
138+
int idx = fileNames.indexOf(QRegExp(QRegExp::escape(current.fileName())));
139+
if (idx > 0) {
140+
showImage(dir.absoluteFilePath(fileNames.at(idx - 1)));
141+
}
142+
else {
143+
QMessageBox::information(this, "Information", "Current image is the first one.");
144+
}
145+
}
146+
147+
void MainWindow::nextImage()
148+
{
149+
QFileInfo current(currentImagePath);
150+
QDir dir = current.absoluteDir();
151+
QStringList nameFilters;
152+
nameFilters << "*.png" << "*.bmp" << "*.jpg";
153+
QStringList fileNames = dir.entryList(nameFilters, QDir::Files, QDir::Name);
154+
int idx = fileNames.indexOf(QRegExp(QRegExp::escape(current.fileName())));
155+
if (idx < fileNames.size() - 1) {
156+
showImage(dir.absoluteFilePath(fileNames.at(idx + 1)));
157+
}
158+
else {
159+
QMessageBox::information(this, "Information", "Current image is the last one.");
160+
}
161+
}
162+
163+
void MainWindow::saveAs()
164+
{
165+
if (currentImage == nullptr) {
166+
QMessageBox::information(this, "Information", "Nothing to save.");
167+
return;
168+
}
169+
QFileDialog dialog(this);
170+
dialog.setWindowTitle("Save Image As ...");
171+
dialog.setFileMode(QFileDialog::AnyFile);
172+
dialog.setAcceptMode(QFileDialog::AcceptSave);
173+
dialog.setNameFilter(tr("Images (*.png *.bmp *.jpg)"));
174+
QStringList fileNames;
175+
if (dialog.exec()) {
176+
fileNames = dialog.selectedFiles();
177+
if (QRegExp(".+\\.(png|bmp|jpg)").exactMatch(fileNames.at(0))) {
178+
currentImage->pixmap().save(fileNames.at(0));
179+
}
180+
else {
181+
QMessageBox::information(this, "Information", "Save error: bad format or filename.");
182+
}
183+
}
184+
}
185+
186+
187+
void MainWindow::setupShortcuts()
188+
{
189+
QList<QKeySequence> shortcuts;
190+
shortcuts << Qt::Key_Plus << Qt::Key_Equal;
191+
zoomInAction->setShortcuts(shortcuts);
192+
193+
shortcuts.clear();
194+
shortcuts << Qt::Key_Minus << Qt::Key_Underscore;
195+
zoomOutAction->setShortcuts(shortcuts);
196+
197+
shortcuts.clear();
198+
shortcuts << Qt::Key_Up << Qt::Key_Left;
199+
prevAction->setShortcuts(shortcuts);
200+
201+
shortcuts.clear();
202+
shortcuts << Qt::Key_Down << Qt::Key_Right;
203+
nextAction->setShortcuts(shortcuts);
204+
}

0 commit comments

Comments
 (0)