From 6c3933ccf497e2f5100bde6a1e68c73c2fdae0e0 Mon Sep 17 00:00:00 2001 From: chenyong Date: Sun, 25 Sep 2016 21:50:55 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E6=8F=92=E4=BB=B6=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- feiq.pro | 9 +++++-- feiqwin.cpp | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ feiqwin.h | 43 ++++++++++++++++++++++++++++++++ main.cpp | 15 ++++------- mainwindow.cpp | 21 ++++++---------- mainwindow.h | 10 +++++--- plugin/iplugin.h | 21 ++++++++++++++++ plugin/unreadchecker.cpp | 34 +++++++++++++++++++++++++ plugin/unreadchecker.h | 24 ++++++++++++++++++ 9 files changed, 212 insertions(+), 30 deletions(-) create mode 100644 feiqwin.cpp create mode 100644 feiqwin.h create mode 100644 plugin/iplugin.h create mode 100644 plugin/unreadchecker.cpp create mode 100644 plugin/unreadchecker.h diff --git a/feiq.pro b/feiq.pro index 2c96e59..e4fddbc 100644 --- a/feiq.pro +++ b/feiq.pro @@ -54,7 +54,9 @@ SOURCES += main.cpp\ chooseemojidlg.cpp \ platformdepend.cpp \ chooseemojiwidget.cpp \ - sendtextedit.cpp + sendtextedit.cpp \ + feiqwin.cpp \ + plugin/unreadchecker.cpp HEADERS += mainwindow.h \ @@ -88,7 +90,10 @@ HEADERS += mainwindow.h \ chooseemojidlg.h \ platformdepend.h \ chooseemojiwidget.h \ - sendtextedit.h + sendtextedit.h \ + plugin/iplugin.h \ + feiqwin.h \ + plugin/unreadchecker.h FORMS += mainwindow.ui \ searchfellowdlg.ui \ diff --git a/feiqwin.cpp b/feiqwin.cpp new file mode 100644 index 0000000..0d452f2 --- /dev/null +++ b/feiqwin.cpp @@ -0,0 +1,65 @@ +#include "feiqwin.h" +#include "mainwindow.h" +#include "plugin/iplugin.h" + +#include "plugin/unreadchecker.h" + +FeiqWin::FeiqWin() +{ + +} + +RecvTextEdit *FeiqWin::recvTextEdit() +{ + return mMainWin->mRecvTextEdit; +} + +SendTextEdit *FeiqWin::sendTextEdit() +{ + return mMainWin->mSendTextEdit; +} + +FellowListWidget *FeiqWin::fellowListWidget() +{ + return &(mMainWin->mFellowList); +} + +const FeiqModel *FeiqWin::feiqModel() +{ + return &(mMainWin->mFeiq.getModel()); +} + +QSettings *FeiqWin::settings() +{ + return mMainWin->mSettings; +} + +int FeiqWin::getUnreadCount() +{ + return mMainWin->getUnreadCount(); +} + +void FeiqWin::init(MainWindow *mainWin) +{ + mMainWin = mainWin; + + loadPlugins(); + + for (auto plugin : mPlugins) + plugin->init(this); +} + +void FeiqWin::unInit() +{ + for (auto plugin : mPlugins) + { + plugin->unInit(); + delete plugin; + } + mPlugins.clear(); +} + +void FeiqWin::loadPlugins() +{ + mPlugins.append(new UnreadChecker()); +} diff --git a/feiqwin.h b/feiqwin.h new file mode 100644 index 0000000..47f03f6 --- /dev/null +++ b/feiqwin.h @@ -0,0 +1,43 @@ +#ifndef FEIQWIN_H +#define FEIQWIN_H + +#include "recvtextedit.h" +#include "sendtextedit.h" +#include "qsettings.h" +#include "fellowlistwidget.h" +#include "feiqlib/feiqmodel.h" + +class MainWindow; +class IPlugin; + +/** + * @brief The FeiqWin class + * 其实就是MainWindow的代言人,用来隔离插件和MainWindow,方便控制MainWindow对插件的可见性 + */ +class FeiqWin +{ +public: + FeiqWin(); + +public: + RecvTextEdit* recvTextEdit(); + SendTextEdit* sendTextEdit(); + FellowListWidget* fellowListWidget(); + const FeiqModel* feiqModel(); + QSettings* settings(); + +public: + int getUnreadCount(); + +private: + void init(MainWindow* mainWin); + void unInit(); + void loadPlugins(); + +private: + friend class MainWindow; + MainWindow* mMainWin; + QList mPlugins; +}; + +#endif // FEIQWIN_H diff --git a/main.cpp b/main.cpp index db382a1..91dbd51 100644 --- a/main.cpp +++ b/main.cpp @@ -1,21 +1,16 @@ #include "mainwindow.h" #include -#include +#include "feiqwin.h" -//待测: -//*无当前交谈用户,且有未读消息时,未读消息能否正确置顶? - -//应用图标 int main(int argc, char *argv[]) { QApplication a(argc, argv); a.setWindowIcon(QIcon(":/default/res/icon.png")); + MainWindow w; + FeiqWin feiqWin; + w.setFeiqWin(&feiqWin); + w.show(); return a.exec(); } - -//表情->html -//查通讯录功能 -//查ip占用功能 -//美化用户列表 diff --git a/mainwindow.cpp b/mainwindow.cpp index 4b9c8b6..7e50a90 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -11,6 +11,7 @@ #include "addfellowdialog.h" #include #include "platformdepend.h" +#include "feiqwin.h" MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), @@ -68,10 +69,6 @@ MainWindow::MainWindow(QWidget *parent) : mTitle = mSettings->value("app/title", "mac飞秋").toString(); setWindowTitle(mTitle); - mUnreadTimerInterval = mSettings->value("app/unread_timer", "0").toInt(); - if (mUnreadTimerInterval > 0) - mUnreadTimerId = startTimer(mUnreadTimerInterval*1000, Qt::VeryCoarseTimer); - //初始化飞秋引擎 connect(this, SIGNAL(feiqViewEvent(shared_ptr)), this, SLOT(handleFeiqViewEvent(shared_ptr))); @@ -90,6 +87,12 @@ MainWindow::~MainWindow() delete ui; } +void MainWindow::setFeiqWin(FeiqWin *feiqWin) +{ + mFeiqWin = feiqWin; + mFeiqWin->init(this); +} + void MainWindow::enterEvent(QEvent *event) { auto fellow = mRecvTextEdit->curFellow(); @@ -102,16 +105,6 @@ void MainWindow::enterEvent(QEvent *event) PlatformDepend::instance().hideAllNotify(); } -void MainWindow::timerEvent(QTimerEvent *event) -{ - if (event->timerId() == mUnreadTimerId) - { - auto count = getUnreadCount(); - if (count > 0) - PlatformDepend::instance().showNotify("未读提醒", QString("还有%1条未读消息").arg(count)); - } -} - void MainWindow::openChartTo(const Fellow *fellow) { mFellowList.top(*fellow); diff --git a/mainwindow.h b/mainwindow.h index 0939ef5..fa14666 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -19,18 +19,22 @@ namespace Ui { class MainWindow; } +class FeiqWin; + Q_DECLARE_METATYPE(shared_ptr) class MainWindow : public QMainWindow, IFeiqView { Q_OBJECT + friend class FeiqWin; public: explicit MainWindow(QWidget *parent = 0); ~MainWindow(); + void setFeiqWin(FeiqWin* feiqWin); + protected: void enterEvent(QEvent *event); - void timerEvent(QTimerEvent *event); signals: void showErrorAndQuit(const QString& text); @@ -92,9 +96,7 @@ private: SendTextEdit* mSendTextEdit; QString mTitle; unordered_map>> mUnreadEvents; - - int mUnreadTimerInterval; - int mUnreadTimerId; + FeiqWin* mFeiqWin = nullptr; }; #endif // MAINWINDOW_H diff --git a/plugin/iplugin.h b/plugin/iplugin.h new file mode 100644 index 0000000..15c623a --- /dev/null +++ b/plugin/iplugin.h @@ -0,0 +1,21 @@ +#ifndef IPLUGIN_H +#define IPLUGIN_H + +#include "../feiqwin.h" + +class IPlugin +{ +public: + virtual ~IPlugin(){} + virtual void init(FeiqWin* feiqWin) + { + mFeiq = feiqWin; + } + + virtual void unInit() = 0; + +protected: + FeiqWin* mFeiq; +}; + +#endif // IPLUGIN_H diff --git a/plugin/unreadchecker.cpp b/plugin/unreadchecker.cpp new file mode 100644 index 0000000..9676576 --- /dev/null +++ b/plugin/unreadchecker.cpp @@ -0,0 +1,34 @@ +#include "unreadchecker.h" +#include +#include "platformdepend.h" + +UnreadChecker::UnreadChecker() +{ + +} + +void UnreadChecker::timerEvent(QTimerEvent *event) +{ + if (event->timerId() == mUnreadTimerId) + { + auto count = mFeiq->getUnreadCount(); + if (count > 0) + PlatformDepend::instance().showNotify("未读提醒", QString("还有%1条未读消息").arg(count)); + } +} + +void UnreadChecker::init(FeiqWin *feiqWin) +{ + IPlugin::init(feiqWin); + + auto settings = mFeiq->settings(); + mUnreadTimerInterval = settings->value("app/unread_timer", "0").toInt(); + if (mUnreadTimerInterval > 0) + mUnreadTimerId = startTimer(mUnreadTimerInterval*1000, Qt::VeryCoarseTimer); +} + +void UnreadChecker::unInit() +{ + if (mUnreadTimerId > 0) + killTimer(mUnreadTimerId); +} diff --git a/plugin/unreadchecker.h b/plugin/unreadchecker.h new file mode 100644 index 0000000..31c868e --- /dev/null +++ b/plugin/unreadchecker.h @@ -0,0 +1,24 @@ +#ifndef UNREADCHECKER_H +#define UNREADCHECKER_H + +#include "iplugin.h" +#include + +class UnreadChecker : public QObject, public IPlugin +{ + Q_OBJECT + +public: + UnreadChecker(); + void init(FeiqWin* feiqWin) override; + void unInit() override; + +protected: + void timerEvent(QTimerEvent *event) override; + +private: + int mUnreadTimerInterval; + int mUnreadTimerId=-1; +}; + +#endif // UNREADCHECKER_H