diff --git a/AnimationBuilder/Animation.cpp b/AnimationBuilder/Animation.cpp new file mode 100644 index 0000000..496c56c --- /dev/null +++ b/AnimationBuilder/Animation.cpp @@ -0,0 +1,163 @@ +#include "Animation.h" +#include "mainwindow.h" + +TAnimation::TAnimation(QObject *parent) + : QObject(parent) +{ +} + +void TAnimation::SetMainWindow(MainWindow* window) +{ + Window = window; +} + + +void TAnimation::AddFrame(const TFrame& frame) +{ + FrameList.push_back(frame); +} + + +TFrame& TAnimation::GetFrame(int i) +{ + return FrameList[i]; +} + +size_t TAnimation::GetFrameCount() +{ + return FrameList.size(); +} + +int TAnimation::GetMaxWidth() +{ + int r = FrameList[0].Pixmap->width() + FrameList[0].ShiftX; + + for (size_t i = 1; i < FrameList.size(); i++) + { + if (r < FrameList[i].Pixmap->width() + FrameList[i].ShiftX) + { + r = FrameList[i].Pixmap->width() + FrameList[i].ShiftX; + } + } + + return r; +} + +int TAnimation::GetMaxHeight() +{ + int r = FrameList[0].Pixmap->height() + FrameList[0].ShiftY; + + for (size_t i = 1; i < FrameList.size(); i++) + { + if (r < FrameList[i].Pixmap->height() + FrameList[i].ShiftY) + { + r = FrameList[i].Pixmap->height() + FrameList[i].ShiftY; + } + } + + return r; +} + + +void TAnimation::FillSheet(boost::shared_ptr sheetPixmap, std::vector& frameDataArr) +{ + int maxFrameWidth = GetMaxWidth(); + int maxFrameHeight = GetMaxHeight(); + + int cursorX = 0; + int cursorY = 0; + + + + QPainter p; + + + p.begin(&(*sheetPixmap)); + + + for (size_t i=0; i(cursorX) / sheetPixmap->width(); + frameData.TexCoordFromY = 1.f - static_cast(cursorY + maxFrameHeight) / sheetPixmap->height(); + + frameData.TexCoordToX = static_cast(cursorX + maxFrameWidth) / sheetPixmap->width(); + frameData.TexCoordToY = 1.f - static_cast(cursorY) / sheetPixmap->height(); + + frameData.TimeToPlay = FrameList[i].TimeToPlay; + + frameDataArr.push_back(frameData); + + + + + QPixmap& pixmapToDraw = *(FrameList[i].Pixmap); + + p.drawPixmap(cursorX + shiftX, cursorY + shiftY, pixmapToDraw); + + cursorX += maxFrameWidth; + + if (cursorX + maxFrameWidth >= sheetPixmap->width()) + { + cursorX = 0; + cursorY += maxFrameHeight; + } + + + } + + p.end(); + + + + + + + +} + + +void TAnimation::LoadFrame() +{ + QFileDialog dialog(Window); + + dialog.setFileMode(QFileDialog::ExistingFiles); + + dialog.setNameFilter("PNG images (*.png)"); + + if (dialog.exec()) + { + + QStringList fileNames = dialog.selectedFiles(); + + for (int i = 0; i < fileNames.count(); i++) + { + boost::shared_ptr pixmap(new QPixmap(fileNames[i], "PNG")); + + QFileInfo q; + q.setFile(fileNames[i]); + + QString fn = q.fileName(); + + std::string fileName = fn.toUtf8().constData(); + + AddFrame(TFrame(pixmap, 0, fileName)); + + Window->LoadFrame(fileName); + } + + //FontFileName = (dialog.selectedFiles())[0]; + //emit SetFontNameTextSignal(FontFileName); + + //LoadFontFile(); + } +} + + + + diff --git a/AnimationBuilder/Animation.h b/AnimationBuilder/Animation.h new file mode 100644 index 0000000..191bd0b --- /dev/null +++ b/AnimationBuilder/Animation.h @@ -0,0 +1,111 @@ +#ifndef ANIMATION_H +#define ANIMATION_H + +#include +#include +#include +#include +#include +#include +#include +#include "boost/shared_ptr.hpp" + + +struct TFrame +{ + boost::shared_ptr Pixmap; + size_t TimeToPlay; + std::string FrameName; + int ShiftX; + int ShiftY; + + TFrame(boost::shared_ptr pixmap, size_t timeToPlay, const std::string& frameName) + : Pixmap(pixmap) + , TimeToPlay(timeToPlay) + , FrameName(frameName) + , ShiftX(0) + , ShiftY(0) + { + } + + TFrame(const TFrame& frame) + { + Pixmap = frame.Pixmap; + TimeToPlay = frame.TimeToPlay; + FrameName = frame.FrameName; + ShiftX = frame.ShiftX; + ShiftY = frame.ShiftY; + } + + TFrame& operator=(const TFrame& frame) + { + if (&frame == this) + { + return *this; + } + + Pixmap = frame.Pixmap; + TimeToPlay = frame.TimeToPlay; + FrameName = frame.FrameName; + ShiftX = frame.ShiftX; + ShiftY = frame.ShiftY; + + return *this; + } + +}; + + +struct TFrameData +{ + float TexCoordFromX; + float TexCoordFromY; + float TexCoordToX; + float TexCoordToY; + size_t TimeToPlay; +}; + +class MainWindow; + + + + +class TAnimation : public QObject +{ + Q_OBJECT + +protected: + + std::vector FrameList; + + MainWindow* Window; + + +public: + + explicit TAnimation(QObject *parent = 0); + + void SetMainWindow(MainWindow* window); + + void AddFrame(const TFrame& frame); + + TFrame& GetFrame(int i); + size_t GetFrameCount(); + + int GetMaxWidth(); + int GetMaxHeight(); + + void FillSheet(boost::shared_ptr sheetPixmap, std::vector& frameDataArr); + + +public slots: + void LoadFrame(); + + + + + +}; + + +#endif // ANIMATION_H diff --git a/AnimationBuilder/AnimationBuilder.pro b/AnimationBuilder/AnimationBuilder.pro new file mode 100644 index 0000000..280d12c --- /dev/null +++ b/AnimationBuilder/AnimationBuilder.pro @@ -0,0 +1,23 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-04-19T21:10:48 +# +#------------------------------------------------- + +QT += core gui + +TARGET = AnimationBuilder +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + Animation.cpp + +HEADERS += mainwindow.h \ + Animation.h + +INCLUDEPATH += $$(LibsPath)/boost_1_47_0 +DEPENDPATH += $$(LibsPath)/boost_1_47_0 + +FORMS += mainwindow.ui diff --git a/AnimationBuilder/main.cpp b/AnimationBuilder/main.cpp new file mode 100644 index 0000000..544d91e --- /dev/null +++ b/AnimationBuilder/main.cpp @@ -0,0 +1,19 @@ +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + + TAnimation Animation; + + MainWindow w; + + w.SetAnimation(&Animation); + Animation.SetMainWindow(&w); + + w.show(); + + + return a.exec(); +} diff --git a/AnimationBuilder/mainwindow.cpp b/AnimationBuilder/mainwindow.cpp new file mode 100644 index 0000000..171ac44 --- /dev/null +++ b/AnimationBuilder/mainwindow.cpp @@ -0,0 +1,294 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + //Animation.SetMainWindow(this); + + frameImageDefaultPosX = ui->FrameImage->pos().x(); + frameImageDefaultPosY = ui->FrameImage->pos().y(); + + //int sheetDefaultWidth = (ui->SheetWidth->text()).toInt(); + //int sheetDefaultHeight = (ui->SheetHeight->text()).toInt(); + + SheetPixmap = boost::shared_ptr(new QPixmap(4, 4)); + + + +} + +MainWindow::~MainWindow() +{ + delete ui; +} + + +int MainWindow::GetSelectedFrame() +{ + QListWidgetItem * item = *(ui->FrameList->selectedItems().begin()); + + int it=0; + + for (int i=0; iFrameList->count(); i++) + { + if (ui->FrameList->item(i) == item) + { + it = i; + } + } + + return it; +} + +void MainWindow::SetAnimation(TAnimation* animation) +{ + Animation = animation; + + QObject::connect(ui->LoadFrameButton, SIGNAL(pressed()), Animation, SLOT(LoadFrame())); + QObject::connect(ui->FrameList, SIGNAL(itemSelectionChanged()), this, SLOT(ItemClicked())); + QObject::connect(ui->ShiftX_spinBox, SIGNAL(valueChanged(int)), this, SLOT(ShiftXChanged(int))); + QObject::connect(ui->ShiftY_spinBox, SIGNAL(valueChanged(int)), this, SLOT(ShiftYChanged(int))); + QObject::connect(ui->TimeFrameEdit, SIGNAL(textChanged(const QString&)), this, SLOT(TimeFrameChanged(const QString&))); + QObject::connect(ui->GenerateTimeButton, SIGNAL(pressed()), this, SLOT(GenerateTimeFrameBy12Fps())); + QObject::connect(ui->GenerateSheetButton, SIGNAL(pressed()), this, SLOT(GenerateSheet())); + QObject::connect(ui->SaveSheetButton, SIGNAL(pressed()), this, SLOT(SaveSheet())); + QObject::connect(ui->SaveAnimParamsButton, SIGNAL(pressed()), this, SLOT(SaveAnimationParams())); + QObject::connect(ui->AnimateCheckBox, SIGNAL(stateChanged(int)), this, SLOT(AnimateCheckBoxStateChanged(int))); + QObject::connect(&AnimTimer, SIGNAL(timeout()), this, SLOT(TimerTimeout())); + + + + +} + + +void MainWindow::ItemClicked() +{ + SelectFrame(GetSelectedFrame()); +} + +void MainWindow::ShiftXChanged(int val) +{ + int i = GetSelectedFrame(); + + Animation->GetFrame(i).ShiftX = val; + + ItemClicked(); +} + + +void MainWindow::ShiftYChanged(int val) +{ + int i = GetSelectedFrame(); + + Animation->GetFrame(i).ShiftY = val; + + ItemClicked(); + +} + +void MainWindow::TimeFrameChanged(const QString& str) +{ + bool ok; + + int timeFrame = str.toInt(&ok); + + if (ok) + { + int i = GetSelectedFrame(); + + Animation->GetFrame(i).TimeToPlay = timeFrame; + } + else + { + } +} + +void MainWindow::GenerateTimeFrameBy12Fps() +{ + float timeStep = 1000.f / 12.f; + + int frameCount = ui->FrameList->count(); + + for (int i=0; i(timeStep * (i + 1)); + + Animation->GetFrame(i).TimeToPlay = totalTime; + + } + + + ItemClicked(); + +} + + +void MainWindow::GenerateSheet() +{ + int sheetWidth = (ui->SheetWidth->text()).toInt(); + int sheetHeight = (ui->SheetHeight->text()).toInt(); + + std::vector frameDataArr; + + SheetPixmap = boost::shared_ptr(new QPixmap(sheetWidth, sheetHeight)); + + QColor transparentColor(0,0,0,0); + + SheetPixmap->fill(transparentColor); + + Animation->FillSheet(SheetPixmap, frameDataArr); + + ui->PixmapSheet->setPixmap(*SheetPixmap); + + + + + + ui->OutputXmlFile->clear(); + + AnimationParamsText = "\n\t\n"; + + for (size_t i = 0; i < frameDataArr.size(); i++) + { + AnimationParamsText += QString("\t\t%5\n") + .arg(QString::number(frameDataArr[i].TexCoordFromX)) + .arg(QString::number(frameDataArr[i].TexCoordFromY)) + .arg(QString::number(frameDataArr[i].TexCoordToX)) + .arg(QString::number(frameDataArr[i].TexCoordToY)) + .arg(QString::number(frameDataArr[i].TimeToPlay)); + } + + AnimationParamsText += "\t\n"; + ui->OutputXmlFile->appendPlainText(AnimationParamsText); + +} + +void MainWindow::SaveSheet() +{ + QFileDialog dialog(this); + + dialog.setAcceptMode(QFileDialog::AcceptSave); + + dialog.setFileMode(QFileDialog::AnyFile); + + dialog.setNameFilter("PNG images (*.png)"); + + dialog.setDefaultSuffix("png"); + + if (dialog.exec()) + { + QString fileName = *(dialog.selectedFiles().begin()); + + SheetPixmap->save(fileName, "PNG"); + } +} + + + +void MainWindow::SaveAnimationParams() +{ + QFileDialog dialog(this); + + dialog.setAcceptMode(QFileDialog::AcceptSave); + + dialog.setFileMode(QFileDialog::AnyFile); + + dialog.setNameFilter("XML files (*.xml)"); + + dialog.setDefaultSuffix("xml"); + + if (dialog.exec()) + { + QString fileName = *(dialog.selectedFiles().begin()); + + QFile file(fileName); + if (file.open(QIODevice::WriteOnly | QIODevice::Text)) + { + QTextStream outStream(&file); + outStream << AnimationParamsText; + } + + } +} + +void MainWindow::AnimateCheckBoxStateChanged(int state) +{ + if (state == Qt::Checked) + { + currentFrameOnTimer = 0; + + + + AnimTimer.setInterval(Animation->GetFrame(currentFrameOnTimer).TimeToPlay); + AnimTimer.start(); + } + else + { + AnimTimer.stop(); + } + +} + +void MainWindow::TimerTimeout() +{ + + + int prevFrameTime = Animation->GetFrame(currentFrameOnTimer).TimeToPlay; + + currentFrameOnTimer++; + + if (currentFrameOnTimer >= Animation->GetFrameCount()) + { + currentFrameOnTimer = 0; + } + + TFrame& frame = Animation->GetFrame(currentFrameOnTimer); + + int timeToPlay = frame.TimeToPlay - prevFrameTime; + + if (timeToPlay < 0) + { + timeToPlay = 0; + } + + + int newPosX = frameImageDefaultPosX + frame.ShiftX; + int newPosY = frameImageDefaultPosY + frame.ShiftY; + + ui->FrameImage->move(newPosX, newPosY); + ui->FrameImage->setPixmap(*(frame.Pixmap)); + + AnimTimer.setInterval(timeToPlay); + AnimTimer.start(); + +} + +void MainWindow::SelectFrame(int i) +{ + TFrame& frame = Animation->GetFrame(i); + + ui->FrameImage->setPixmap(*(frame.Pixmap)); + + int newPosX = frameImageDefaultPosX + frame.ShiftX; + int newPosY = frameImageDefaultPosY + frame.ShiftY; + + ui->FrameImage->move(newPosX, newPosY); + + ui->ShiftX_spinBox->setValue(frame.ShiftX); + ui->ShiftY_spinBox->setValue(frame.ShiftY); + + ui->TimeFrameEdit->setText(QString::number(frame.TimeToPlay)); + +} + +void MainWindow::LoadFrame(const std::string& text) +{ + ui->FrameList->addItem(QString(text.c_str())); + +} diff --git a/AnimationBuilder/mainwindow.h b/AnimationBuilder/mainwindow.h new file mode 100644 index 0000000..9a8298b --- /dev/null +++ b/AnimationBuilder/mainwindow.h @@ -0,0 +1,66 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include +#include + + +#include "Animation.h" + +namespace Ui { +class MainWindow; +} + + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + + void SetAnimation(TAnimation* animation); + + + +public slots: + void SelectFrame(int i); + void LoadFrame(const std::string& text); + void ItemClicked(); + + void ShiftXChanged(int val); + void ShiftYChanged(int val); + void TimeFrameChanged(const QString& str); + + void GenerateTimeFrameBy12Fps(); + void GenerateSheet(); + + void SaveSheet(); + void SaveAnimationParams(); + + void AnimateCheckBoxStateChanged(int state); + void TimerTimeout(); + +private: + Ui::MainWindow *ui; + + TAnimation* Animation; + + int frameImageDefaultPosX; + int frameImageDefaultPosY; + + boost::shared_ptr SheetPixmap; + QString AnimationParamsText; + + QTimer AnimTimer; + int currentFrameOnTimer; + + int GetSelectedFrame(); + + //void SetTimerToNextFrame(); +}; + +#endif // MAINWINDOW_H diff --git a/AnimationBuilder/mainwindow.ui b/AnimationBuilder/mainwindow.ui new file mode 100644 index 0000000..b7ea69b --- /dev/null +++ b/AnimationBuilder/mainwindow.ui @@ -0,0 +1,390 @@ + + + MainWindow + + + + 0 + 0 + 700 + 600 + + + + + 631 + 442 + + + + + 700 + 600 + + + + + 0 + 0 + + + + AnimationBuilder + + + + + + 10 + 30 + 111 + 23 + + + + Add frames... + + + + + + 170 + 60 + 171 + 161 + + + + QFrame::StyledPanel + + + QFrame::Plain + + + + + 10 + 10 + 151 + 141 + + + + + + + + + + + 10 + 60 + 151 + 191 + + + + + + + 490 + 60 + 42 + 22 + + + + 0 + + + + + + 490 + 90 + 42 + 22 + + + + 0 + + + + + + 410 + 60 + 71 + 20 + + + + shift on X: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 410 + 90 + 71 + 20 + + + + shift on Y: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 490 + 120 + 71 + 20 + + + + 0 + + + + + + 410 + 120 + 71 + 20 + + + + time to play: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 90 + 260 + 71 + 20 + + + + 512 + + + + + + 90 + 290 + 71 + 20 + + + + 256 + + + + + + 20 + 260 + 71 + 20 + + + + Sheet width: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 20 + 290 + 71 + 20 + + + + Sheet height: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 30 + 320 + 131 + 23 + + + + Generate sheet + + + + + + 420 + 150 + 141 + 23 + + + + Generate by 12 fps + + + + + + 180 + 250 + 512 + 256 + + + + + + + + + + 180 + 250 + 511 + 261 + + + + QFrame::StyledPanel + + + QFrame::Plain + + + + + + 20 + 350 + 141 + 121 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAlwaysOn + + + QPlainTextEdit::NoWrap + + + + + + 30 + 480 + 131 + 23 + + + + Save sheet... + + + + + + 30 + 510 + 131 + 23 + + + + Save anim params... + + + + + + 170 + 230 + 70 + 17 + + + + Animate + + + frame_2 + LoadFrameButton + frame + FrameList + ShiftX_spinBox + ShiftY_spinBox + label + label_2 + TimeFrameEdit + label_3 + SheetWidth + SheetHeight + label_4 + label_5 + GenerateSheetButton + GenerateTimeButton + PixmapSheet + OutputXmlFile + SaveSheetButton + SaveAnimParamsButton + AnimateCheckBox + + + + TopToolBarArea + + + false + + + + + + + 0 + 0 + 700 + 21 + + + + + + + + diff --git a/Bn1An1Converter/Bn1An1Converter.pro b/Bn1An1Converter/Bn1An1Converter.pro new file mode 100644 index 0000000..6bf9336 --- /dev/null +++ b/Bn1An1Converter/Bn1An1Converter.pro @@ -0,0 +1,18 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-06-01T11:21:21 +# +#------------------------------------------------- + +QT += core gui + +TARGET = Bn1An1Converter +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp + +HEADERS += mainwindow.h + +FORMS += mainwindow.ui diff --git a/Bn1An1Converter/main.cpp b/Bn1An1Converter/main.cpp new file mode 100644 index 0000000..d1378ad --- /dev/null +++ b/Bn1An1Converter/main.cpp @@ -0,0 +1,11 @@ +#include +#include "mainwindow.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + return a.exec(); +} diff --git a/Bn1An1Converter/mainwindow.cpp b/Bn1An1Converter/mainwindow.cpp new file mode 100644 index 0000000..944daa1 --- /dev/null +++ b/Bn1An1Converter/mainwindow.cpp @@ -0,0 +1,387 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include +#include +#include + + +struct TBoneStruct +{ + float v[3]; + float q[4]; + float len; + + QString parentBoneName; +}; + + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + QObject::connect(ui->ConvertAn1, SIGNAL(clicked()), this, SLOT(CallConvertAn1())); + QObject::connect(ui->ConvertBn1, SIGNAL(clicked()), this, SLOT(CallConvertBn1())); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + + +void MainWindow::CallConvertAn1() +{ + QFileDialog dialog(this); + dialog.setFileMode(QFileDialog::AnyFile); + dialog.setAcceptMode(QFileDialog::AcceptOpen); + + if (dialog.exec()) + { + QMessageBox msgBox1; + msgBox1.setText("Begin"); + msgBox1.exec(); + + QFile file(dialog.selectedFiles()[0]); + if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) + { + QMessageBox msgBox; + msgBox.setText("Fail"); + msgBox.exec(); + return; + } + + + + + QFileDialog dialogSave(this); + dialogSave.setFileMode(QFileDialog::AnyFile); + dialogSave.setAcceptMode(QFileDialog::AcceptSave); + + if (!dialogSave.exec()) + { + return; + } + + QByteArray saveArr; + + + QFile saveFile(dialogSave.selectedFiles()[0]); + if (!saveFile.open(QIODevice::WriteOnly | QIODevice::Truncate)) + { + QMessageBox msgBox; + msgBox.setText("Fail"); + msgBox.exec(); + return; + } + + + QTextStream in(&file); + QString line; + + //======= Go file parsing + + saveArr.push_back('A'); + saveArr.push_back('N'); + saveArr.push_back((char)0); + saveArr.push_back((char)1); + + line = in.readLine(); + + int frameCount = line.remove("Frames ").toInt(); + + line = in.readLine(); + + int boneCount = line.remove("Bones ").toInt(); + + saveArr.insert(saveArr.size(), (char*)&boneCount, 4); + saveArr.insert(saveArr.size(), (char*)&frameCount, 4); + + QStringList splited; + + float v[3]; + float q[4]; + + float len; + + for (int i=0; i boneNameMap; + + //======= Go file parsing + + line = in.readLine(); + + boneCount = line.remove("Bones ").toInt(); + + std::vector boneStructMap; + + for (int i=0; i + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + +public slots: + void CallConvertAn1(); + void CallConvertBn1(); + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/Bn1An1Converter/mainwindow.ui b/Bn1An1Converter/mainwindow.ui new file mode 100644 index 0000000..bc28054 --- /dev/null +++ b/Bn1An1Converter/mainwindow.ui @@ -0,0 +1,67 @@ + + + MainWindow + + + + 0 + 0 + 400 + 300 + + + + MainWindow + + + + + + 10 + 20 + 121 + 31 + + + + Convert An1 + + + + + + 10 + 60 + 121 + 31 + + + + Convert Bn1 + + + + + + + 0 + 0 + 400 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/FreeTypeWorker-win/release/FreeTypeWorker.exe b/FreeTypeWorker-win/release/FreeTypeWorker.exe new file mode 100644 index 0000000..de177d1 Binary files /dev/null and b/FreeTypeWorker-win/release/FreeTypeWorker.exe differ diff --git a/FreeTypeWorker-win/release/QtCore4.dll b/FreeTypeWorker-win/release/QtCore4.dll new file mode 100644 index 0000000..813845d Binary files /dev/null and b/FreeTypeWorker-win/release/QtCore4.dll differ diff --git a/FreeTypeWorker-win/release/QtGui4.dll b/FreeTypeWorker-win/release/QtGui4.dll new file mode 100644 index 0000000..ccc25d9 Binary files /dev/null and b/FreeTypeWorker-win/release/QtGui4.dll differ diff --git a/FreeTypeWorker-win/release/msvcp100.dll b/FreeTypeWorker-win/release/msvcp100.dll new file mode 100644 index 0000000..6f0cdf1 Binary files /dev/null and b/FreeTypeWorker-win/release/msvcp100.dll differ diff --git a/FreeTypeWorker-win/release/msvcr100.dll b/FreeTypeWorker-win/release/msvcr100.dll new file mode 100644 index 0000000..b1c3a5e Binary files /dev/null and b/FreeTypeWorker-win/release/msvcr100.dll differ diff --git a/FreeTypeWorker/FreeTypeWorker.pro b/FreeTypeWorker/FreeTypeWorker.pro new file mode 100644 index 0000000..0fd20d7 --- /dev/null +++ b/FreeTypeWorker/FreeTypeWorker.pro @@ -0,0 +1,30 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-01-08T09:45:02 +# +#------------------------------------------------- + +QT += core gui + +TARGET = FreeTypeWorker +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + freetypemodel.cpp + +HEADERS += mainwindow.h \ + freetypemodel.h + +FORMS += mainwindow.ui + +win32:CONFIG(release, debug|release): LIBS += -L$$(LibsPath)/freetype-2.4.7/objs/win32/vc2010/ -lfreetype247MT +else:win32:CONFIG(debug, debug|release): LIBS += -L$$(LibsPath)/freetype-2.4.7/objs/win32/vc2010/ -lfreetype247MT_D + +INCLUDEPATH += $$(LibsPath)/freetype-2.4.7/include +DEPENDPATH += $$(LibsPath)/freetype-2.4.7/include + + +win32:CONFIG(release, debug|release): PRE_TARGETDEPS += $$(LibsPath)/freetype-2.4.7/objs/win32/vc2010/freetype247MT.lib +else:win32:CONFIG(debug, debug|release): PRE_TARGETDEPS += $$(LibsPath)/freetype-2.4.7/objs/win32/vc2010/freetype247MT_D.lib diff --git a/FreeTypeWorker/freetypemodel.cpp b/FreeTypeWorker/freetypemodel.cpp new file mode 100644 index 0000000..d424a0e --- /dev/null +++ b/FreeTypeWorker/freetypemodel.cpp @@ -0,0 +1,318 @@ +#include "freetypemodel.h" +#include "mainwindow.h" + + +TFreeTypeModel::TFreeTypeModel(QObject *parent) : + QObject(parent) + , FontFileName("") +{ + fontColor = Qt::white; + backgroundColor = Qt::transparent; + + fontInited = false; + +} + +TFreeTypeModel::~TFreeTypeModel() +{ + +} + +void TFreeTypeModel::SetMainWindow(MainWindow* aMainWindow) +{ + mainWindow = aMainWindow; +} + + +void TFreeTypeModel::ClearFontParamText() +{ + fontParamText.clear(); +} + +QColor TFreeTypeModel::GetBackgroundColor() +{ + return backgroundColor; +} + +QColor TFreeTypeModel::GetFontColor() +{ + return fontColor; +} + +void TFreeTypeModel::RenderFontSlot() +{ + ProcessFont(); +} + +void TFreeTypeModel::LoadFont() +{ + + QFileDialog dialog(mainWindow); + + dialog.setNameFilter(tr("TTF (*.ttf)")); + + dialog.setFileMode(QFileDialog::ExistingFile); + + if (dialog.exec()) + { + FontFileName = (dialog.selectedFiles())[0]; + + QFileInfo fileInfo(FontFileName); + + emit SetFontNameTextSignal(fileInfo.fileName()); + //emit SetFontNameTextSignal(FontFileName); + + LoadFontFile(); + + } +} + +void TFreeTypeModel::SaveFont() +{ + QFileDialog dialog(mainWindow); + + dialog.setFileMode(QFileDialog::AnyFile); + + dialog.setAcceptMode(QFileDialog::AcceptSave); + if (dialog.exec()) + { + QString str = (dialog.selectedFiles())[0]; + mainWindow->Pixmap->save(str + ".png", "PNG"); + mainWindow->SaveOutputEdit(fontParamText, str + ".txt"); + + } +} + +void TFreeTypeModel::SelectBackgroundColorPressed() +{ + QColorDialog dialog(mainWindow); + + dialog.setOption(QColorDialog::ShowAlphaChannel); + + if (dialog.exec()) + { + backgroundColor = dialog.currentColor(); + mainWindow->RefreshColorMarkers(); + } +} + +void TFreeTypeModel::SelectFontColorPressed() +{ + QColorDialog dialog(mainWindow); + + dialog.setOption(QColorDialog::ShowAlphaChannel); + + if (dialog.exec()) + { + fontColor = dialog.currentColor(); + mainWindow->RefreshColorMarkers(); + } +} + +void TFreeTypeModel::LoadFontFile() +{ + try + { + if (fontInited) + { + DestroyFontFile(); + } + + int error = FT_Init_FreeType(&FreeTypeLibrary); + + if (error) + { + throw QString("FT_Init_FreeType failed"); + } + + emit SetLoadResultTextSignal("Successful"); + + mainWindow->UnlockDrawButton(); + + fontInited = true; + } + catch(QString errorString) + { + emit SetLoadResultTextSignal(errorString); + } +} + +void TFreeTypeModel::DestroyFontFile() +{ + FT_Done_FreeType(FreeTypeLibrary); +} + + +void TFreeTypeModel::ProcessAndDrawSymbol(unsigned long charcode, QImage& img, int& shiftX, int& shiftY) +{ + + FT_Face face; + + int error = FT_New_Face(FreeTypeLibrary, FontFileName.toAscii(), 0, &face); + + if (error == FT_Err_Unknown_File_Format) + { + throw QString("FT_New_Face failed: FT_Err_Unknown_File_Format"); + } + else if (error) + { + throw QString("FT_New_Face failed: other error"); + } + + //Not in use + //int intervalX = mainWindow->GetIntervalX(); + //int intervalY = mainWindow->GetIntervalY(); + + int faceWidth = mainWindow->GetFaceWidth(); + int faceHeight = mainWindow->GetFaceHeight(); + + if (FT_Set_Pixel_Sizes(face, faceWidth, faceHeight)) + { + throw QString("FT_Set_Pixel_Sizes failed"); + } + + unsigned int glyph_index = FT_Get_Char_Index( face, charcode ); + + if (FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER)) + { + throw QString("FT_Load_Glyph failed"); + } + + FT_Glyph_Metrics metrics = face->glyph->metrics; + + int internalShiftY = mainWindow->GetFaceHeight() - metrics.horiBearingY / 64; + int internalShiftX = metrics.horiBearingX / 64; + + int bitmapWidth = face->glyph->bitmap.width; + int bitmapHeight = face->glyph->bitmap.rows; + + int oldShiftX = shiftX; + int oldShiftY = shiftY; + + int advance = face->glyph->advance.x / 64; + + ProcessSymbol(face, charcode, shiftX, shiftY); + + for (int i=0; iglyph->bitmap.buffer[(i+j*face->glyph->bitmap.width)]; + QColor finalColor; + + + finalColor.setRed(backgroundColor.red()*(1.f - c/255.f) + (fontColor.red()) * (c/255.f)); + finalColor.setGreen(backgroundColor.green()*(1.f - c/255.f) + (fontColor.green()) * (c/255.f)); + finalColor.setBlue(backgroundColor.blue()*(1.f - c/255.f) + (fontColor.blue()) * (c/255.f)); + finalColor.setAlpha(backgroundColor.alpha()*(1.f - c/255.f) + (fontColor.alpha()) * (c/255.f)); + + img.setPixel(i+oldShiftX, + j+oldShiftY+internalShiftY, + finalColor.rgba()); + } + } + + float areaWidth = mainWindow->GetAreaWidth(); + float areaHeight = mainWindow->GetAreaHeight(); + + QString qstr = QString::number(charcode) + " " + + QString::number(oldShiftX / areaWidth) + " " + + QString::number(oldShiftY / areaHeight) + " " + + QString::number(internalShiftX / areaWidth) + " " + + QString::number(internalShiftY / areaHeight) + " " + + QString::number(bitmapWidth / areaWidth) + " " + + QString::number(bitmapHeight / areaHeight) + " " + + QString::number(advance / areaWidth); + + + fontParamText.push_back(qstr); + + FT_Done_Face(face); +} + + + +void TFreeTypeModel::ProcessSymbol(FT_Face& face, unsigned long charcode, int& shiftX, int& shiftY) +{ + + int intervalX = mainWindow->GetIntervalX(); + int intervalY = mainWindow->GetIntervalY(); + + int faceWidth = mainWindow->GetFaceWidth(); + int faceHeight = mainWindow->GetFaceHeight(); + + int error = FT_Set_Pixel_Sizes(face, faceWidth, faceHeight); + + if (error) + { + throw QString("FT_Set_Pixel_Sizes failed"); + } + + unsigned int glyph_index = FT_Get_Char_Index(face, charcode); + + + error = FT_Load_Glyph(face, glyph_index, FT_LOAD_RENDER); + + + if (error) + { + throw QString("FT_Set_Pixel_Sizes failed"); + } + + int bitmapWidth = face->glyph->bitmap.width; + + if (shiftX + 2*(bitmapWidth + intervalX)+intervalX >= mainWindow->GetAreaWidth()) + { + shiftX = intervalX; + shiftY += mainWindow->GetFaceHeight() + intervalY; + } + else + { + shiftX += bitmapWidth + intervalX; + } +} + + + +void TFreeTypeModel::ProcessFont() +{ + try + { + + mainWindow->RefreshSheetHeightWidth(); + + QImage img(mainWindow->GetAreaWidth(), mainWindow->GetAreaHeight(), QImage::Format_ARGB32); + + img.fill(backgroundColor.rgba()); + + int intervalX = mainWindow->GetIntervalX(); + int intervalY = mainWindow->GetIntervalY(); + + int shiftX = intervalX; + int shiftY = intervalY; + + QString str = mainWindow->GetSymbolList(); + + + ClearFontParamText(); + + + for (int i=0; iPixmap->convertFromImage(img, Qt::DiffuseAlphaDither); + mainWindow->Pixmap->convertFromImage(img/*, Qt::DiffuseAlphaDither*/); + + emit InvalidateSignal(); + emit SetLoadResultTextSignal("Successful"); + + mainWindow->UnlockSaveButton(); + } + catch(QString errorString) + { + emit SetLoadResultTextSignal(errorString); + } +} diff --git a/FreeTypeWorker/freetypemodel.h b/FreeTypeWorker/freetypemodel.h new file mode 100644 index 0000000..6bed852 --- /dev/null +++ b/FreeTypeWorker/freetypemodel.h @@ -0,0 +1,65 @@ +#ifndef FREETYPEMODEL_H +#define FREETYPEMODEL_H + +#include +#include + +#include +#include FT_FREETYPE_H +#include FT_GLYPH_H + +class MainWindow; + +class TFreeTypeModel : public QObject +{ + Q_OBJECT + + QString FontFileName; + + MainWindow* mainWindow; + + FT_Library FreeTypeLibrary; + + std::vector fontParamText; + + QColor backgroundColor; + QColor fontColor; + + bool fontInited; + + void LoadFontFile(); + void DestroyFontFile(); + + void ProcessSymbol(FT_Face& face, unsigned long charcode, int& shiftX, int& shiftY); + void ProcessAndDrawSymbol(unsigned long charcode, QImage& img, int& shiftX, int& shiftY); + + void ProcessFont(); + +public: + explicit TFreeTypeModel(QObject *parent = 0); + + ~TFreeTypeModel(); + + void SetMainWindow(MainWindow* aMainWindow); + + void ClearFontParamText(); + + QColor GetBackgroundColor(); + QColor GetFontColor(); + +signals: + void SetFontNameTextSignal(QString text); + void SetLoadResultTextSignal(QString text); + void InvalidateSignal(); + //void AddRowToOutputTextSignal(QString text); + +public slots: + void LoadFont(); + void RenderFontSlot(); + void SaveFont(); + + void SelectBackgroundColorPressed(); + void SelectFontColorPressed(); +}; + +#endif // FREETYPEMODEL_H diff --git a/FreeTypeWorker/main.cpp b/FreeTypeWorker/main.cpp new file mode 100644 index 0000000..a5ef036 --- /dev/null +++ b/FreeTypeWorker/main.cpp @@ -0,0 +1,22 @@ +#include +#include "mainwindow.h" +#include "freetypemodel.h" + +int main(int argc, char *argv[]) +{ + TFreeTypeModel FreeTypeModel; + + QApplication a(argc, argv); + MainWindow w; + w.show(); + + w.SetFreeTypeModel(&FreeTypeModel); + FreeTypeModel.SetMainWindow(&w); + + QObject::connect(&FreeTypeModel, SIGNAL(SetFontNameTextSignal(QString)), &w, SLOT(SetFontNameTextSlot(QString))); + QObject::connect(&FreeTypeModel, SIGNAL(SetLoadResultTextSignal(QString)), &w, SLOT(SetLoadResultTextSlot(QString))); + QObject::connect(&FreeTypeModel, SIGNAL(InvalidateSignal()), &w, SLOT(InvalidatePixmap())); + + + return a.exec(); +} diff --git a/FreeTypeWorker/mainwindow.cpp b/FreeTypeWorker/mainwindow.cpp new file mode 100644 index 0000000..4d8928e --- /dev/null +++ b/FreeTypeWorker/mainwindow.cpp @@ -0,0 +1,148 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); + + Pixmap = new QPixmap(128, 128); + +} + +MainWindow::~MainWindow() +{ + delete Pixmap; + delete ui; +} + +void MainWindow::SetFreeTypeModel(TFreeTypeModel* freeTypeModel) +{ + FreeTypeModel = freeTypeModel; + + //Set signals + QObject::connect(ui->LoadFontButton, SIGNAL(pressed()), FreeTypeModel, SLOT(LoadFont())); + QObject::connect(ui->RenderImageButton, SIGNAL(pressed()), FreeTypeModel, SLOT(RenderFontSlot())); + QObject::connect(ui->SaveButton, SIGNAL(pressed()), FreeTypeModel, SLOT(SaveFont())); + + QObject::connect(ui->selectBackgroundColor, SIGNAL(pressed()), FreeTypeModel, SLOT(SelectBackgroundColorPressed())); + + QObject::connect(ui->selectFontColor, SIGNAL(pressed()), FreeTypeModel, SLOT(SelectFontColorPressed())); + + + RefreshColorMarkers(); +} + +void MainWindow::SetFontNameTextSlot(QString text) +{ + ui->FontFileNameLabel->setText("File name: "+text); +} + +void MainWindow::SetLoadResultTextSlot(QString text) +{ + ui->LoadLibResultLabel->setText("Loading result: "+text); +} + +void MainWindow::InvalidatePixmap() +{ + ui->Image->setPixmap(*Pixmap); +} + +int MainWindow::GetFaceWidth() +{ + return ui->faceWidthEdit->text().toInt(); +} + +int MainWindow::GetFaceHeight() +{ + return ui->faceHeightEdit->text().toInt(); +} + +int MainWindow::GetAreaWidth() +{ + return ui->Image->width(); +} + +int MainWindow::GetAreaHeight() +{ + return ui->Image->height(); +} + +int MainWindow::GetIntervalX() +{ + return ui->IntervalXEdit->text().toInt(); +} + +int MainWindow::GetIntervalY() +{ + return ui->IntervalYEdit->text().toInt(); +} + +QString MainWindow::GetSymbolList() +{ + return ui->CharList->toPlainText(); +} + +void MainWindow::RefreshSheetHeightWidth() +{ + int width = (ui->SheetWidthEdit->text()).toInt(); + int height = (ui->SheetHeightEdit->text()).toInt(); + + QRect r(ui->frame->geometry()); + r.setWidth(width); + r.setHeight(height); + ui->frame->setGeometry(r); + + r = QRect(ui->Image->geometry()); + r.setWidth(width); + r.setHeight(height); + ui->Image->setGeometry(r); + +} + +void MainWindow::SaveOutputEdit(std::vector& fontParamText, QString filename) +{ + + QFile file(filename); + if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) + return; + + QTextStream out(&file); + for (std::vector::iterator i = fontParamText.begin(); i != fontParamText.end(); i++) + { + out << *i<<"\n"; + } + + file.close(); + +} + +void MainWindow::UnlockDrawButton() +{ + ui->RenderImageButton->setEnabled(true); +} + +void MainWindow::UnlockSaveButton() +{ + ui->SaveButton->setEnabled(true); +} + +void MainWindow::RefreshColorMarkers() +{ + QPixmap* colorPixmap = new QPixmap(21,21); + + colorPixmap->fill(FreeTypeModel->GetBackgroundColor()); + ui->backgroundColorMarker->setPixmap(*colorPixmap); + + colorPixmap->fill(FreeTypeModel->GetFontColor()); + ui->fontColorMarker->setPixmap(*colorPixmap); + + delete colorPixmap; + + + //Pixmap also: + Pixmap->fill(FreeTypeModel->GetBackgroundColor()); + +} + diff --git a/FreeTypeWorker/mainwindow.h b/FreeTypeWorker/mainwindow.h new file mode 100644 index 0000000..d3c5da2 --- /dev/null +++ b/FreeTypeWorker/mainwindow.h @@ -0,0 +1,55 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include "freetypemodel.h" + +namespace Ui { +class MainWindow; +} + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + QPixmap* Pixmap; + + + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + + void SetFreeTypeModel(TFreeTypeModel* freeTypeModel); + + int GetFaceWidth(); + int GetFaceHeight(); + + int GetAreaWidth(); + int GetAreaHeight(); + + int GetIntervalX(); + int GetIntervalY(); + + QString GetSymbolList(); + + void RefreshSheetHeightWidth(); + + void SaveOutputEdit(std::vector& fontParamText, QString filename); + + void UnlockDrawButton(); + void UnlockSaveButton(); + + void RefreshColorMarkers(); + +private: + Ui::MainWindow *ui; + TFreeTypeModel* FreeTypeModel; + +public slots: + void SetFontNameTextSlot(QString text); + void SetLoadResultTextSlot(QString text); + void InvalidatePixmap(); +}; + +#endif // MAINWINDOW_H diff --git a/FreeTypeWorker/mainwindow.ui b/FreeTypeWorker/mainwindow.ui new file mode 100644 index 0000000..fbe1f85 --- /dev/null +++ b/FreeTypeWorker/mainwindow.ui @@ -0,0 +1,471 @@ + + + MainWindow + + + + 0 + 0 + 540 + 540 + + + + + 0 + 0 + + + + + 540 + 540 + + + + + 16777215 + 16777215 + + + + + 540 + 540 + + + + FreeTypeWorker + + + + + + + + 0 + 0 + + + + + + 10 + 10 + 171 + 23 + + + + 1. Load font... + + + + + + 10 + 40 + 171 + 81 + + + + + QFormLayout::AllNonFixedFieldsGrow + + + + + File name: + + + + + + + Loading result: + + + + + + + + + 190 + 40 + 341 + 71 + + + + Qt::ScrollBarAlwaysOn + + + Qt::ScrollBarAlwaysOff + + + <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> +<html><head><meta name="qrichtext" content="1" /><style type="text/css"> +p, li { white-space: pre-wrap; } +</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> +<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;"> 1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.,:;@#$%^&amp;*!?()[]{}&lt;&gt;_-+=|\/~`&quot;'</span></p></body></html> + + + + + + 190 + 20 + 101 + 16 + + + + Symbols to draw: + + + + + + 270 + 120 + 71 + 20 + + + + 32 + + + + + + 430 + 120 + 71 + 20 + + + + 32 + + + + + + 190 + 120 + 71 + 20 + + + + Face width: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + false + + + + 10 + 130 + 171 + 23 + + + + + 0 + 0 + + + + 2. Draw + + + + + + 190 + 150 + 71 + 20 + + + + X spacing: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 270 + 150 + 71 + 20 + + + + 5 + + + + + + 430 + 150 + 71 + 20 + + + + 5 + + + + + + 10 + 230 + 512 + 256 + + + + QFrame::StyledPanel + + + QFrame::Raised + + + + + 0 + 0 + 512 + 256 + + + + 1 + + + + + + + + + false + + + + 10 + 160 + 171 + 23 + + + + 3. Save... + + + + + + 430 + 180 + 71 + 20 + + + + 256 + + + + + + 180 + 180 + 81 + 16 + + + + Sheet width: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 270 + 180 + 71 + 20 + + + + 512 + + + + + + 340 + 180 + 81 + 16 + + + + Sheet height: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 350 + 150 + 71 + 20 + + + + Y spacing: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 350 + 120 + 71 + 20 + + + + Face height: + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + 214 + 200 + 121 + 23 + + + + Background color... + + + + + + 380 + 200 + 121 + 23 + + + + Font color... + + + + + + 340 + 200 + 21 + 21 + + + + + + + + + + 510 + 200 + 21 + 21 + + + + + + + frame + LoadFontButton + formLayoutWidget + CharList + label_7 + faceWidthEdit + faceHeightEdit + label_8 + RenderImageButton + label_10 + IntervalXEdit + IntervalYEdit + SaveButton + SheetHeightEdit + label_12 + SheetWidthEdit + label_13 + label_11 + label_9 + selectBackgroundColor + selectFontColor + backgroundColorMarker + fontColorMarker + + + + + 0 + 0 + 540 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + diff --git a/K-observer_client_qt/CustomButton.h b/K-observer_client_qt/CustomButton.h new file mode 100644 index 0000000..442835f --- /dev/null +++ b/K-observer_client_qt/CustomButton.h @@ -0,0 +1,47 @@ +#ifndef CUSTOMBUTTON_H +#define CUSTOMBUTTON_H + +#include + +#include "observerclientmodel.h" + +class TCustomButton : public QPushButton +{ + Q_OBJECT +public: + + explicit TCustomButton(QWidget *parent=0) + : QPushButton(parent) + { + } + + explicit TCustomButton(const QString &text, QWidget *parent=0) + : QPushButton(text, parent) + { + } + + + //TObserverClientModel* ObserverClientModel; + + boost::function PressedHandler; + +protected: + + virtual void mouseReleaseEvent ( QMouseEvent * e ) + { + QAbstractButton::mouseReleaseEvent(e); +/* + if (ObserverClientModel != NULL) + { + ObserverClientModel->OnConnectButtonPressed(); + }*/ + + PressedHandler(); + + + + } + +}; + +#endif // CUSTOMBUTTON_H diff --git a/K-observer_client_qt/K-observer_client.pro b/K-observer_client_qt/K-observer_client.pro new file mode 100644 index 0000000..76cde5f --- /dev/null +++ b/K-observer_client_qt/K-observer_client.pro @@ -0,0 +1,42 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2012-08-22T11:32:24 +# +#------------------------------------------------- + +QT += core gui +QT += webkit + +CONFIG += no_keywords + +TARGET = K-observer_client +TEMPLATE = app + + +SOURCES += main.cpp\ + mainwindow.cpp \ + ../../k_observer/common/UserInfo.cpp \ + ../../k_observer/common/misc.cpp \ + ../../k_observer/common/MessageSender.cpp \ + observerclientmodel.cpp \ + ../../k_observer/common/ClientSocket.cpp \ + customtimer.cpp + +HEADERS += mainwindow.h \ + ../../k_observer/common/UserInfo.h \ + ../../k_observer/common/misc.h \ + ../../k_observer/common/MessageSender.h \ + observerclientmodel.h \ + ../../k_observer/common/ClientSocket.h \ + CustomButton.h \ + customtimer.h + +INCLUDEPATH += C:/Workplace/libs/boost_1_47_0/ +INCLUDEPATH += ../../k_observer/common/ + +LIBS += -LC:/Workplace/libs/boost_1_47_0/boost_windows/libs_engine/debug/ +LIBS += -LC:/Workplace/libs/boost_1_47_0/boost_windows/libs_engine/release/ + + + +FORMS += mainwindow.ui diff --git a/K-observer_client_qt/Release/release/K-observer_client.exe b/K-observer_client_qt/Release/release/K-observer_client.exe new file mode 100644 index 0000000..48fb89f Binary files /dev/null and b/K-observer_client_qt/Release/release/K-observer_client.exe differ diff --git a/K-observer_client_qt/Release/release/QtCore4.dll b/K-observer_client_qt/Release/release/QtCore4.dll new file mode 100644 index 0000000..813845d Binary files /dev/null and b/K-observer_client_qt/Release/release/QtCore4.dll differ diff --git a/K-observer_client_qt/Release/release/QtGui4.dll b/K-observer_client_qt/Release/release/QtGui4.dll new file mode 100644 index 0000000..ccc25d9 Binary files /dev/null and b/K-observer_client_qt/Release/release/QtGui4.dll differ diff --git a/K-observer_client_qt/Release/release/QtNetwork4.dll b/K-observer_client_qt/Release/release/QtNetwork4.dll new file mode 100644 index 0000000..d1eee2d Binary files /dev/null and b/K-observer_client_qt/Release/release/QtNetwork4.dll differ diff --git a/K-observer_client_qt/Release/release/QtWebKit4.dll b/K-observer_client_qt/Release/release/QtWebKit4.dll new file mode 100644 index 0000000..77a7e81 Binary files /dev/null and b/K-observer_client_qt/Release/release/QtWebKit4.dll differ diff --git a/K-observer_client_qt/customtimer.cpp b/K-observer_client_qt/customtimer.cpp new file mode 100644 index 0000000..c0824ee --- /dev/null +++ b/K-observer_client_qt/customtimer.cpp @@ -0,0 +1,15 @@ +#include "customtimer.h" + +#include "observerclientmodel.h" + +CustomTimer::CustomTimer(QObject *parent) + : QTimer(parent) +{ +} + + +void CustomTimer::timerEvent ( QTimerEvent * e ) +{ + + TimerSignal(); +} diff --git a/K-observer_client_qt/customtimer.h b/K-observer_client_qt/customtimer.h new file mode 100644 index 0000000..3fc492e --- /dev/null +++ b/K-observer_client_qt/customtimer.h @@ -0,0 +1,20 @@ +#ifndef CUSTOMTIMER_H +#define CUSTOMTIMER_H + +#include +#include +#include "UserInfo.h" +class CustomTimer : public QTimer +{ + Q_OBJECT +public: + explicit CustomTimer(QObject *parent = 0); + + boost::signal TimerSignal; + +protected: + virtual void timerEvent ( QTimerEvent * e ); + +}; + +#endif // CUSTOMTIMER_H diff --git a/K-observer_client_qt/main.cpp b/K-observer_client_qt/main.cpp new file mode 100644 index 0000000..c5eb6f5 --- /dev/null +++ b/K-observer_client_qt/main.cpp @@ -0,0 +1,31 @@ +#include +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include "observerclientmodel.h" + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + + TObserverClientModel observerClient; + + w.ObserverClientModel = &observerClient; + observerClient.MainWindowPtr = &w; + + w.OpenMap(); + + w.GetUi()->ConnectButton->PressedHandler = boost::bind(&TObserverClientModel::OnConnectButtonPressed, &observerClient); + w.GetUi()->UpdateDataButton->PressedHandler = boost::bind(&TObserverClientModel::OnUpdateInfoButtonPressed, &observerClient); + + + w.GetUi()->DisconnectButton->PressedHandler = boost::bind(&TObserverClientModel::Finish, &observerClient); + + w.GetUi()->MapTypeButton->PressedHandler = boost::bind(&TObserverClientModel::OnChangeMapTypeButtonPressed, &observerClient); + + w.GetUi()->ChatSendButton->PressedHandler = boost::bind(&TObserverClientModel::OnChatSendClick, &observerClient); + + + return a.exec(); +} diff --git a/K-observer_client_qt/mainwindow.cpp b/K-observer_client_qt/mainwindow.cpp new file mode 100644 index 0000000..cb979ed --- /dev/null +++ b/K-observer_client_qt/mainwindow.cpp @@ -0,0 +1,32 @@ +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +MainWindow::MainWindow(QWidget *parent) : + QMainWindow(parent), + ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + //ObserverClientModel->Finish(); + delete ui; +} + + +void MainWindow::OpenMap() +{ + QUrl url("C:/Workplace/Projects/Qt/K-observer_client/script.html"); + //QUrl url("../K-observer_client/script.html"); + //QUrl url("script.html"); + + + ui->webView->settings()->setAttribute(QWebSettings::JavascriptEnabled, true); + ui->webView->load(url); + + ui->webView->page()->setContentEditable(true); + + //ui->webView->page()->mainFrame()->evaluateJavaScript("this.AddMarker(37.443195, 55.888869);"); +} diff --git a/K-observer_client_qt/mainwindow.h b/K-observer_client_qt/mainwindow.h new file mode 100644 index 0000000..e2f3278 --- /dev/null +++ b/K-observer_client_qt/mainwindow.h @@ -0,0 +1,35 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +namespace Ui { +class MainWindow; +} + +class TObserverClientModel; + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = 0); + ~MainWindow(); + + + + Ui::MainWindow* GetUi() + { + return ui; + } + + void OpenMap(); + + TObserverClientModel* ObserverClientModel; + +private: + Ui::MainWindow *ui; +}; + +#endif // MAINWINDOW_H diff --git a/K-observer_client_qt/mainwindow.ui b/K-observer_client_qt/mainwindow.ui new file mode 100644 index 0000000..e71a785 --- /dev/null +++ b/K-observer_client_qt/mainwindow.ui @@ -0,0 +1,462 @@ + + + MainWindow + + + + 0 + 0 + 646 + 543 + + + + MainWindow + + + + + + 10 + 30 + 91 + 23 + + + + Подключиться + + + + + + 10 + 10 + 111 + 20 + + + + 127.0.0.1 + + + + + + 10 + 110 + 113 + 20 + + + + Иванов + + + + + + 10 + 90 + 46 + 13 + + + + Фамилия: + + + + + + 10 + 140 + 46 + 13 + + + + Имя: + + + + + + 10 + 160 + 113 + 20 + + + + Иван + + + + + + 10 + 190 + 71 + 16 + + + + Отчество: + + + + + + 10 + 210 + 113 + 20 + + + + Иванович + + + + + + 10 + 240 + 121 + 16 + + + + Контактный телефон: + + + + + + 10 + 260 + 113 + 20 + + + + 1111 + + + + + + 10 + 310 + 111 + 22 + + + + + + + 10 + 290 + 121 + 16 + + + + Номер УИК: + + + + + + 10 + 360 + 131 + 22 + + + + + Наблюдатель + + + + + Мобильная группа + + + + + + + 10 + 340 + 121 + 16 + + + + Статус: + + + + + + 10 + 390 + 131 + 23 + + + + Обновить информацию + + + + + + 220 + 10 + 401 + 251 + + + + + about:blank + + + + + + + 262 + 270 + 81 + 22 + + + + 6 + + + 0.001000000000000 + + + 37.443195000000003 + + + + + + 372 + 270 + 81 + 22 + + + + 6 + + + 0.010000000000000 + + + 55.888869000000000 + + + + + + 242 + 270 + 16 + 16 + + + + X: + + + + + + 352 + 270 + 16 + 16 + + + + Y: + + + + + + 462 + 270 + 151 + 22 + + + + + Состояние норм + + + + + Состояние тревога + + + + + + + 432 + 310 + 181 + 23 + + + + Послать сигнал тревоги + + + + + + 130 + 10 + 61 + 20 + + + + 1984 + + + + + + 110 + 30 + 75 + 23 + + + + PushButton + + + + + + 332 + 300 + 75 + 23 + + + + Выбрать + + + + + + 230 + 300 + 91 + 22 + + + + + Общая карта + + + + + Карта тревоги + + + + + + + 230 + 340 + 381 + 111 + + + + Qt::ScrollBarAlwaysOn + + + true + + + + + + 230 + 460 + 301 + 20 + + + + + + + 540 + 460 + 75 + 23 + + + + Отправить + + + + + + + 0 + 0 + 646 + 21 + + + + + + TopToolBarArea + + + false + + + + + + + + QWebView + QWidget +
QtWebKit/QWebView
+
+ + TCustomButton + QPushButton +
custombutton.h
+
+
+ + +
diff --git a/K-observer_client_qt/observerclientmodel.cpp b/K-observer_client_qt/observerclientmodel.cpp new file mode 100644 index 0000000..31f8907 --- /dev/null +++ b/K-observer_client_qt/observerclientmodel.cpp @@ -0,0 +1,328 @@ +#include "observerclientmodel.h" +#include "mainwindow.h" +#include "ui_mainwindow.h" +#include + +QString +ws2qs(const std::wstring& str) +{ + return (QString((const QChar*)str.c_str(), str.length())); +} + +// Convert a QString to a wstring +std::wstring +qs2ws(const QString& str) +{ + return (std::wstring((wchar_t*)str.unicode(), str.length())); +} + + +TObserverClientModel::TObserverClientModel(QObject *parent) + : QObject(parent) + , MapType(0) +{ +} + + +void TObserverClientModel::Start(std::string address, std::string port) +{ + ClientSocket = boost::shared_ptr(new TClientSocket(IoService)); + + ClientSocket->Open(address, port); + + RestartHeartbeatTimer(); + RestartMapHeartbeatTimer(); + + IoServiceThread = boost::thread(boost::bind(&boost::asio::io_service::run, &IoService)); + + ClientSocket->ReceiveMapSignal.connect(boost::bind(&TObserverClientModel::OnReceiveMapUpdate, this, _1)); + + ClientSocket->ReceiveAlarmMapSignal.connect(boost::bind(&TObserverClientModel::OnReceiveAlarmMapUpdate, this, _1)); + + + ClientSocket->ReceiveCloseSignal.connect(boost::bind(&TObserverClientModel::OnSocketClosed, this)); + + ClientSocket->ReceiveMessageSignal.connect(boost::bind(&TObserverClientModel::OnReceiveChatMessage, this, _1, _2)); + + + + Timer.TimerSignal.connect(boost::bind(&TObserverClientModel::OnUpdateMapTimer, this)); + + Timer.start(1000); +} + +void TObserverClientModel::RestartHeartbeatTimer() +{ + HeartbeatTimer = boost::shared_ptr(new boost::asio::deadline_timer(IoService, boost::posix_time::seconds(3))); + + HeartbeatTimer->async_wait(boost::bind(&TObserverClientModel::OnHeartbeat, this, boost::asio::placeholders::error)); + +} + +void TObserverClientModel::RestartMapHeartbeatTimer() +{ + MapHeartbeatTimer = boost::shared_ptr(new boost::asio::deadline_timer(IoService, boost::posix_time::seconds(3))); + + MapHeartbeatTimer->async_wait(boost::bind(&TObserverClientModel::OnMapHeartbeat, this, boost::asio::placeholders::error)); + +} + + +void TObserverClientModel::Finish() +{ + Timer.stop(); + HeartbeatTimer->cancel(); + MapHeartbeatTimer->cancel(); + //HeartbeatTimer = boost::shared_ptr(); + //MapHeartbeatTimer = boost::shared_ptr(); + + ClientSocket->CloseImmediate(); + + + +} + +void TObserverClientModel::OnSocketClosed() +{ + ClientSocket = boost::shared_ptr(); +} + +void TObserverClientModel::OnUpdateMapTimer() +{ + UpdatingMapMutex.lock(); + + MainWindowPtr->GetUi()->webView->page()->mainFrame()->evaluateJavaScript("Clear();"); + + + if (MapType == 0) + { + + + for (std::vector::iterator i = UserInfoArr.begin(); i != UserInfoArr.end(); ++i) + { + QString posX = QString::number( i->PosX ); + QString posY = QString::number( i->PosY ); + QString mapState = QString::number(i->MapState); + + if (i->PosX != 0.f && i->PosY != 0.f) + { + MainWindowPtr->GetUi()->webView->page()->mainFrame()->evaluateJavaScript("AddMarker("+posX+", "+posY+", "+ mapState + ");"); + } + } + } + else + { + for (std::vector::iterator i = AlarmUserInfoArr.begin(); i != AlarmUserInfoArr.end(); ++i) + { + QString posX = QString::number( i->PosX ); + QString posY = QString::number( i->PosY ); + QString mapState = QString::number(i->MapState); + + + + std::wstring Uik = L" "; + std::wstring StateHeader = L": "; + + QString text = ws2qs(Uik); + text = text + QString::number(i->Uik)+"\\n"; + text = text +QString::fromUtf8(i->LastName.c_str()); + text = text +"\\n" + QString::fromUtf8(i->FirstName.c_str()); + text = text + "\\n"+QString::fromUtf8(i->MiddleName.c_str()); + text = text + "\\n"+QString::fromUtf8(i->PhoneNumber.c_str()); + text = text + "\\n"; + text = text +ws2qs(StateHeader); + + std::wstring State; + + if (i->MapState == 2) + { + State = L""; + } + else if (i->MapState == 2) + { + State = L" "; + } + else if (i->MapState == 3) + { + State = L""; + } + else if (i->MapState == 4) + { + State = L""; + } + else if (i->MapState == 5) + { + State = L""; + } + + text += ws2qs(State); + + if (i->PosX != 0.f && i->PosY != 0.f) + { + MainWindowPtr->GetUi()->webView->page()->mainFrame()->evaluateJavaScript("AddMarkerWithPopup("+posX+", "+posY+", "+ mapState + ", \"" +text+ "\");"); + + //MainWindowPtr->GetUi()->webView->page()->mainFrame()->evaluateJavaScript("AddMarker("+posX+", "+posY+", "+ mapState + ");"); + } + } + } + + Timer.start(1000); + + UpdatingMapMutex.unlock(); +} + + +void TObserverClientModel::OnConnectButtonPressed() +{ + std::string address(MainWindowPtr->GetUi()->AddressField->text().toAscii()); + std::string port(MainWindowPtr->GetUi()->PortField->text().toAscii()); + + Start(address, port); +} + +void TObserverClientModel::OnUpdateInfoButtonPressed() +{ + + std::string lastName(MainWindowPtr->GetUi()->LastNameField->text().toUtf8()); + std::string firstName(MainWindowPtr->GetUi()->FirstNameField->text().toUtf8()); + std::string middleName(MainWindowPtr->GetUi()->MiddleNameField->text().toUtf8()); + + std::string phoneNumber(MainWindowPtr->GetUi()->PhoneNumberField->text().toUtf8()); + + int uik = MainWindowPtr->GetUi()->UikSpinBox->value(); + + int status = MainWindowPtr->GetUi()->StatusComboBox->currentIndex(); + + float posX = MainWindowPtr->GetUi()->PosXSpinBox->value(); + float posY = MainWindowPtr->GetUi()->PosYSpinBox->value(); + + int mapState = MainWindowPtr->GetUi()->MapStateComboBox->currentIndex(); + if (status == 1) + { + mapState = 0; + } + + ClientSocket->UserInfo.LastName = lastName; + ClientSocket->UserInfo.FirstName = firstName; + ClientSocket->UserInfo.MiddleName = middleName; + ClientSocket->UserInfo.PhoneNumber = phoneNumber; + ClientSocket->UserInfo.Uik = uik; + ClientSocket->UserInfo.Status = status; + ClientSocket->UserInfo.MapState = mapState; + + ClientSocket->UserInfo.PosX = posX; + ClientSocket->UserInfo.PosY = posY; + + ClientSocket->SendUserinfoUpdateThreaded(); + +} + + +void TObserverClientModel::OnHeartbeat(const boost::system::error_code& e) +{ + if (e) + { + return; + } + + float posX = MainWindowPtr->GetUi()->PosXSpinBox->value(); + float posY = MainWindowPtr->GetUi()->PosYSpinBox->value(); + + int uik = MainWindowPtr->GetUi()->UikSpinBox->value(); + int status = MainWindowPtr->GetUi()->StatusComboBox->currentIndex(); + int mapState = MainWindowPtr->GetUi()->MapStateComboBox->currentIndex() + 1; + if (status == 1) + { + mapState = 0; + } + + ClientSocket->UserInfo.Uik = uik; + ClientSocket->UserInfo.MapState = mapState; + + ClientSocket->UserInfo.PosX = posX; + ClientSocket->UserInfo.PosY = posY; + + ClientSocket->SendHeartbeatThreaded(); + + RestartHeartbeatTimer(); +} + +void TObserverClientModel::OnMapHeartbeat(const boost::system::error_code& e) +{ + if (e) + { + return; + } + + if (MapType == 0) + { + ClientSocket->SendMapQueryThreaded(); + } + else + { + ClientSocket->SendAlarmMapQueryThreaded(); + } + + RestartMapHeartbeatTimer(); +} + +void TObserverClientModel::OnReceiveMapUpdate(std::vector userInfoArr) +{ + + UpdatingMapMutex.lock(); + + UserInfoArr = userInfoArr; + + UpdatingMapMutex.unlock(); + +} + +void TObserverClientModel::OnReceiveAlarmMapUpdate(std::vector userInfoArr) +{ + UpdatingMapMutex.lock(); + + AlarmUserInfoArr = userInfoArr; + + UpdatingMapMutex.unlock(); +} + + +void TObserverClientModel::OnChangeMapTypeButtonPressed() +{ + MapType = MainWindowPtr->GetUi()->MapTypeComboBox->currentIndex(); + + if (MapType == 0) + { + ClientSocket->SendMapQueryThreaded(); + } + else + { + ClientSocket->SendAlarmMapQueryThreaded(); + } +} + +void TObserverClientModel::OnChatSendClick() +{ + QString msg = MainWindowPtr->GetUi()->ChatTextLine->text(); + + ClientSocket->SendMessageThreaded(std::string(msg.toUtf8())); +} + +void TObserverClientModel::OnReceiveChatMessage(TUserInfo userInfo, std::string msg) +{ + QString qmsg; + + qmsg += QString::fromUtf8(userInfo.LastName.c_str()) + " "; + qmsg += QString::fromUtf8(userInfo.FirstName.c_str()) + " "; + qmsg += QString::fromUtf8(userInfo.MiddleName.c_str()) + " "; + qmsg += QString::fromUtf8(userInfo.PhoneNumber.c_str()) + " "; + qmsg += QString(ws2qs(L" ")) + QString::number(userInfo.Uik) + ": "; + + + qmsg += QString::fromUtf8(msg.c_str()) + "\n"; + + + MainWindowPtr->GetUi()->ChatList->insertPlainText(qmsg); + + +} diff --git a/K-observer_client_qt/observerclientmodel.h b/K-observer_client_qt/observerclientmodel.h new file mode 100644 index 0000000..83115b0 --- /dev/null +++ b/K-observer_client_qt/observerclientmodel.h @@ -0,0 +1,74 @@ +#ifndef OBSERVERCLIENTMODEL_H +#define OBSERVERCLIENTMODEL_H + +#include + +#include "UserInfo.h" +#include "misc.h" +#include "ClientSocket.h" +#include "customtimer.h" + + +class MainWindow; + +class TObserverClientModel : public QObject +{ + Q_OBJECT +public: + explicit TObserverClientModel(QObject *parent = 0); + + MainWindow* MainWindowPtr; + + boost::shared_ptr ClientSocket; + + boost::asio::io_service IoService; + + boost::thread IoServiceThread; + + boost::mutex UpdatingMapMutex; + + std::vector UserInfoArr; + std::vector AlarmUserInfoArr; + + CustomTimer Timer; + + int MapType; + + void Start(std::string address, std::string port); + void Finish(); + + void OnSocketClosed(); + + void RestartHeartbeatTimer(); + void RestartMapHeartbeatTimer(); + + void OnConnectButtonPressed(); + + + + void OnUpdateInfoButtonPressed(); + + void OnChangeMapTypeButtonPressed(); + + void OnHeartbeat(const boost::system::error_code& e); + void OnMapHeartbeat(const boost::system::error_code& e); + + void OnUpdateMapTimer(); + void OnReceiveMapUpdate(std::vector userInfoArr); + void OnReceiveAlarmMapUpdate(std::vector userInfoArr); + + void OnChatSendClick(); + + void OnReceiveChatMessage(TUserInfo userInfo, std::string msg); + + + boost::shared_ptr HeartbeatTimer; + boost::shared_ptr MapHeartbeatTimer; + +//signals: + +//public slots: + +}; + +#endif // OBSERVERCLIENTMODEL_H diff --git a/K-observer_client_qt/script.html b/K-observer_client_qt/script.html new file mode 100644 index 0000000..1824ddd --- /dev/null +++ b/K-observer_client_qt/script.html @@ -0,0 +1,91 @@ + +OpenLayers Simplest Example +
+ + \ No newline at end of file