1. 程式人生 > 其它 >Qt編寫視覺化大屏電子看板系統32-模組10大屏地圖

Qt編寫視覺化大屏電子看板系統32-模組10大屏地圖

一、前言

大屏地圖模組採用瀏覽器模組+echart元件,Qt自帶了webkit或者webengine模組,其中在win上mingw編譯器的Qt5.6以後的版本,沒有了瀏覽器模組,這個就需要用第三方的瀏覽器模組比如miniblink或者cef等,個人偏好miniblink因為足夠簡單。為了能夠相容所有的Qt版本和應用場景,特意專門寫了通用瀏覽器控制元件獨立的類webview,只要new即可,會自動判斷Qt版本以及存在哪種瀏覽器模組就用哪種,優先採用Qt自帶的瀏覽器模組,除非手動指定miniblink就用miniblink瀏覽器模組。

中間大屏地圖多種樣式:

  • 靜態圖片:存在資原始檔中的靜態圖片檔案;
  • 閃爍效果:中國地圖不同城市閃爍點效果;
  • 遷徙效果:中國地圖不同城市飛機遷徙效果;
  • 世界地圖:世界地圖不同國家飛機遷徙效果;
  • 區域地圖:指定某個省市不同縣城閃爍點效果,不同顏色點;

二、功能特點

  1. 採用分層設計,整體總共分三級介面,一級介面是整體佈局,二級介面是單個功能模組,三級介面是單個控制元件。
  2. 子控制元件包括餅圖、圓環圖、曲線圖、柱狀圖、柱狀分組圖、橫向柱狀圖、橫向柱狀分組圖、合格率控制元件、百分比控制元件、進度控制元件、裝置狀態面板、表格資料、地圖控制元件、視訊控制元件等。
  3. 二級介面可以自由拖動懸浮,支援最小化隱藏、最大化關閉、響應雙擊自定義標題欄。
  4. 資料來源支援模擬資料(預設)、資料庫採集、串列埠通訊(需定製)、網路通訊(需定製)、網路請求等,可自由設定每個子介面的採集間隔即資料重新整理頻率。
  5. 採用純QWidget編寫,親測Qt4.6到Qt6.2任意版本,理論上支援後續其他Qt版本。
  6. 超強跨平臺,親測windows、linux、mac、國產uos、國產銀河麒麟kylin等系統,效果完美,同時還支援嵌入式linux比如樹莓派、香橙派、全志、imx6等。
  7. 同時集成了自定義控制元件、qchart餅圖、echart地圖等功能。
  8. 內建多套配色風格樣式(紫色、藍色、深藍、黑色),預設紫色,自適應任意解析度。
  9. 可設定系統標題、目標解析度、佈局方案,啟動立即應用。
  10. 可設定主背景顏色、面板顏色、十字線遊標顏色等各種顏色。
  11. 可設定多條曲線不同顏色,沒有設定顏色的情況下內建多套精美顏色隨機應用。
  12. 可設定標題欄背景顏色、文字顏色。
  13. 可設定曲線圖表背景顏色、文字顏色、網格顏色。
  14. 可設定正常顏色、警戒顏色、報警顏色、禁用顏色、百分比進度顏色。
  15. 可分別設定各種字型大小,比如全域性字型、軟體名稱、標題欄、子標題欄、加粗標籤等。
  16. 可設定標題欄高度、表頭高度、行高度。
  17. 曲線支援遊標、定位線、懸停高亮資料點、懸停顯示值。
  18. 柱狀圖支援頂部(可設定頂端、上部、中間、底部)顯示資料,全部自適應計算位置。
  19. 支援平滑曲線,內建多種平滑曲線演算法,還支援面積圖平滑。
  20. 面積圖填充顏色可選多種規則比如單色透明度填充、透明度漸變填充等。
  21. 資料庫支援sqlite、mysql、postgresql、oracle、國產人大金倉等資料庫。
  22. 主介面直接滑鼠右鍵切換佈局、配色方案、關閉開啟某個二級窗體。
  23. 自動記憶所有子視窗的大小和位置,下次啟動立即應用。
  24. 動態載入佈局方案選單,可以動態新建佈局、恢復佈局、儲存佈局、另存佈局等,使用者可以製造任意佈局。
  25. 二級窗體,雙擊從主窗體分離出來浮動,可以自由調整大小。再次雙擊標題欄最大化,再次雙擊還原。
  26. 子模組也可以全屏顯示作為一個大屏,這樣就可以一個大屏拓展出多個子大屏,放大檢視子模組的資料詳情,適用多屏展示。
  27. 每個模組都可以自定義採集速度,如果是資料庫採集會自動排隊處理,後期還可以拓展每個子模組都獨立的資料庫採集。
  28. 提供系統設定模組進行整體的配置引數設定,效果立即應用。
  29. 提供精美炫酷的大屏地圖模組,包括靜態圖片、閃爍效果、遷徙效果、世界地圖、區域地圖等,可指定點的經緯度座標,識別單擊響應,可以做地圖跳轉等,每個點都可以不同的顏色和提示資訊。
  30. 除了提供大屏系統外,還將每個模組都做了獨立的模組示例介面,每個模組都可以獨立學習使用,裡面用到的控制元件也單獨做了控制元件示例介面,方便學習每個控制元件如何使用。
  31. 非常詳細的開發和使用手冊,其中包括資料庫說明、模組對照圖、控制元件對照圖、專案結構、程式碼說明(精確到每個類)、演示demo、使用方法等。

三、體驗地址

  1. 體驗地址:https://pan.baidu.com/s/1d7TH_GEYl5nOecuNlWJJ7g 提取碼:01jf 檔名:bin_bigscreen.zip。
  2. 國內站點:https://gitee.com/feiyangqingyun
  3. 國際站點:https://github.com/feiyangqingyun
  4. 個人主頁:https://blog.csdn.net/feiyangqingyun
  5. 知乎主頁:https://www.zhihu.com/people/feiyangqingyun/
  6. 線上文件:https://feiyangqingyun.gitee.io/qwidgetdemo/bigscreen/

四、效果圖




五、核心程式碼

#include "frmmodulemap.h"
#include "ui_frmmodulemap.h"
#include "quihelper.h"

#ifdef echarts
#include "echarts.h"
#include "echartjs.h"
#endif

frmModuleMap::frmModuleMap(QWidget *parent) : QWidget(parent), ui(new Ui::frmModuleMap)
{
    ui->setupUi(this);
    this->initForm();
    this->initBg();
    this->changeMapStyle(false);
}

frmModuleMap::~frmModuleMap()
{
    delete ui;
}

void frmModuleMap::showEvent(QShowEvent *)
{
    static bool isShow = false;
    if (!isShow) {
        isShow = true;
        QTimer::singleShot(100, this, SLOT(loadBg()));
    }
}

void frmModuleMap::resizeEvent(QResizeEvent *)
{
    if (AppConfig::MapStyle == "image") {
        loadImage();
    } else {
        resizeMap();
    }
}

void frmModuleMap::initForm()
{
    isLoad = false;
    labMap = new QLabel;
    labMap->setObjectName("labMap");
    labMap->setAlignment(Qt::AlignCenter);
    labMap->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
    this->layout()->addWidget(labMap);
#ifdef echarts
    web = new WebView(this);
    web->setLayout(ui->verticalLayout);
    connect(web, SIGNAL(loadFinished(bool)),
            this, SLOT(loadFinished()));
    connect(web, SIGNAL(receiveDataFromJs(QString, QVariant)),
            this, SLOT(receiveDataFromJs(QString, QVariant)));
#endif

    //樣式改變了需要重新載入地圖,因為顏色變了
    connect(AppEvent::Instance(), SIGNAL(changeStyle()), this, SLOT(loadBg()));
    //地圖樣式改變
    connect(AppEvent::Instance(), SIGNAL(changeMapStyle()), this, SLOT(changeMapStyle()));
}

void frmModuleMap::initBg()
{
#ifdef echarts
    web->setBgColor(AppConfig::ColorMainBg);
#endif
}

void frmModuleMap::loadBg()
{
    if (AppConfig::MapStyle == "image") {
        loadImage();
    } else {
        loadMap();
    }
}

void frmModuleMap::loadImage()
{
    QString fileName = "bg_china1.png";
    if (AppConfig::ThemeName == "紫色風格") {
        fileName = "bg_china1.png";
    } else if (AppConfig::ThemeName == "藍色風格") {
        fileName = "bg_china2.png";
    } else if (AppConfig::ThemeName == "深藍風格") {
        fileName = "bg_china3.png";
    } else if (AppConfig::ThemeName == "黑色風格") {
        fileName = "bg_china4.png";
    }

    //等比例平滑縮放
    QPixmap pix(QString("%1/image_bigscreen/%2").arg(QUIHelper::appPath()).arg(fileName));
    pix = pix.scaled(labMap->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation);
    labMap->setPixmap(pix);
}

void frmModuleMap::loadMap()
{
#ifdef echarts
    QString fileName = QString("%1/map_echarts.html").arg(ConfigPath);
    url = "file:///" + fileName;

    Echarts::Instance()->setHeight(this->height());
    Echarts::Instance()->setFileName(fileName);
    Echarts::Instance()->setSaveFile(SaveFile);

    Echarts::Instance()->setZoom(1.2);
    Echarts::Instance()->setMapJsName("china");
    Echarts::Instance()->setMapAreaName("china");

    QStringList cityName, cityValue, cityPoint, cityColor, cityTip;
    cityName << "上海" << "北京" << "成都" << "武漢" << "廈門" << "廣州";
    cityValue << "250" << "220" << "150" << "180" << "140" << "170";
    cityPoint << "121.48,31.22" << "116.46,39.92" << "104.06,30.67" << "114.31,30.52" << "118.1,24.46" << "113.23,23.16";

    //設定顏色,也可以定義成不同的顏色,牛逼吧
    for (int i = 0; i < cityName.count(); i++) {
        cityColor << AppConfig::ColorPercent;
    }

    //設定提示資訊,為空則按照預設規則,更牛逼吧
    //提示資訊可以是html字串(這樣就強大到爆了) 換行符是 <br>
    for (int i = 0; i < cityName.count(); i++) {
        cityTip << QString("%1 = %2%").arg(cityName.at(i)).arg(cityValue.at(i));
    }

    Echarts::Instance()->setCityName(cityName);
    Echarts::Instance()->setCityValue(cityValue);
    Echarts::Instance()->setCityPoint(cityPoint);
    Echarts::Instance()->setCityColor(cityColor);
    Echarts::Instance()->setCityTip(cityTip);

    QString html;
    if (AppConfig::MapStyle == "point") {
        html = Echarts::Instance()->newChartPoint();
    } else if (AppConfig::MapStyle == "move") {
        cityValue.clear();
        cityValue << "0" << "0" << "0" << "0" << "0" << "1";
        Echarts::Instance()->setCityValue(cityValue);
        html = Echarts::Instance()->newChartMove("北京");
    } else if (AppConfig::MapStyle == "world") {
        loadMapWord();
        html = Echarts::Instance()->newChartMove("中國");
    } else if (AppConfig::MapStyle == "area") {
        //如果要載入省份而不是市則設定 dirName和areaName 都是省份名稱就行
        loadMapArea("江西", "吉安市");
        html = Echarts::Instance()->newChartPoint();
    }

    //下面為兩種方式載入網頁,如果內容為空則載入網頁檔案否則載入內容
    //一般為了保密建議載入內容,這樣看不到生成的網頁檔案
    if (Echarts::Instance()->getSaveFile()) {
        web->load(url);
    } else {
        QString baseUrl = QString("%1/").arg(ConfigPath);
        web->load("", html, baseUrl);
        //QString http = "http://data.fengmap.cn:1024/yz/connect/DEMO/%E6%A8%A1%E5%9E%8B3D/DEMO%E4%B8%89%E7%BB%B41/20180611_%E5%94%90%E5%B1%B1%E5%B7%A5%E4%BA%BA%E5%8C%BB%E9%99%A2_241902";
        //web->load(http);
    }
#endif
}

void frmModuleMap::loadMapWord()
{
    QStringList cityName, cityValue, cityPoint, cityColor, cityTip;
#if 0
    cityName << "美國1" << "美國2" << "美國3" << "英國" << "德國" << "荷蘭" << "澳大利亞" << "溫州";
    cityValue << "0" << "0" << "0" << "0" << "0" << "0" << "0" << "1";
    cityPoint << "-115.652087,44.677279" << "-97.990682,32.358956" << "-119.037189,35.667425"
              << "-1.742162,52.468150" << "10.032107,50.633281" << "5.763934,53.181365"
              << "134.250655,-23.981496" << "120.65,28.01";
#else
    cityName << "北美洲" << "南美洲" << "非洲" << "歐洲" << "大洋洲" << "印度" << "中國";
    cityValue << "0" << "0" << "0" << "0" << "0" << "0" << "1";
    cityPoint << "-101.670961,41.103997" << "-58.842054,-11.895087" << "22.106051,14.099049"
              << "15.777381,49.971800" << "132.637011,-25.596128" << "78.475369,22.934290"
              << "113.651000,39.526776";
#endif

    //設定顏色,也可以定義成不同的顏色,牛逼吧
    for (int i = 0; i < cityName.count(); i++) {
        cityColor << "";
        cityTip << QString("銷售額佔比: %1 = %2%").arg(cityName.at(i)).arg(rand() % 100);
    }

    Echarts::Instance()->setCityName(cityName);
    Echarts::Instance()->setCityValue(cityValue);
    Echarts::Instance()->setCityPoint(cityPoint);
    Echarts::Instance()->setCityColor(cityColor);
    Echarts::Instance()->setCityTip(cityTip);
    Echarts::Instance()->setMapJsName("world");
    Echarts::Instance()->setMapAreaName("world");
}

bool frmModuleMap::loadMapArea(const QString &dirName, const QString &areaName)
{
    QStringList cityName, cityValue, cityPoint, cityColor, cityTip;
    QString jsName = dirName + "/" + areaName;
    QString jsFile = QString("%1/areajs/%2/%3.js").arg(ConfigPath).arg(dirName).arg(areaName);
    //檔案不存在則不用載入
    if (!QFile(jsFile).exists()) {
        return false;
    }

    QStringList infos = EchartJs::getInfoFromJs(jsFile);
    foreach (QString info, infos) {
        QStringList list = info.split("|");
        cityName << list.at(0);
        cityValue << QString("%1").arg((rand() % 150) + 100);
        cityPoint << list.at(1);
    }

    QStringList colors = QColor::colorNames();
    for (int i = 0; i < cityName.count(); i++) {
        cityColor << colors.at(rand() % colors.count());
        cityTip << QString("%1 = %2 產量").arg(cityName.at(i)).arg(cityValue.at(i));
    }

    Echarts::Instance()->setCityName(cityName);
    Echarts::Instance()->setCityValue(cityValue);
    Echarts::Instance()->setCityPoint(cityPoint);
    Echarts::Instance()->setCityColor(cityColor);
    Echarts::Instance()->setCityTip(cityTip);
    Echarts::Instance()->setMapJsName(jsName);
    Echarts::Instance()->setMapAreaName(areaName);
    return true;
}

void frmModuleMap::resizeMap()
{
#ifdef echarts
    if (isLoad) {
        QString js = QString("resize(%1, %2)").arg(this->width()).arg(this->height());
        web->runJs(js);
    }
#endif
}

void frmModuleMap::changeMapStyle(bool load)
{
    if (AppConfig::MapStyle == "image") {
#ifdef echarts
        web->setVisible(false);
#endif
        labMap->setVisible(true);
    } else {
        labMap->setVisible(false);
#ifdef echarts
        web->setVisible(true);
#endif
    }

    if (load) {
        //qApp->processEvents();
        if (AppConfig::MapStyle == "image") {
            loadImage();
        } else {
            loadMap();
        }
    }
}

void frmModuleMap::loadFinished()
{
    isLoad = true;
}

void frmModuleMap::receiveDataFromJs(const QString &type, const QVariant &data)
{
    qDebug() << type << data;
    QString datas = data.toString();

    if (type == "name") {
        //根據單擊的省份重新載入省份地圖
        if (AppConfig::MapStyle == "point" || AppConfig::MapStyle == "move") {
            //如果設定成功則切進去並記住省份名稱
            bool ok = loadMapArea(datas, datas);
            if (ok) {
                QString html = Echarts::Instance()->newChartPoint();
                if (Echarts::Instance()->getSaveFile()) {
                    web->load(url);
                } else {
                    QString baseUrl = QString("%1/").arg(ConfigPath);
                    web->load("", html, baseUrl);
                }
            }
        } else {
            QUIHelper::showMessageBoxInfo(QString("當前單擊了區域 %1").arg(datas));
        }
    } else if (type == "params") {
        //如果單擊的不是點資料則會帶undefined,這樣可以過濾掉不需要的資料
        if (!datas.contains("undefined")) {
            QUIHelper::showMessageBoxInfo(QString("當前單擊了點 %1").arg(datas));
        }
    }
}