diff --git a/feiq.pro b/feiq.pro index 802c0a8..7a6a407 100644 --- a/feiq.pro +++ b/feiq.pro @@ -57,7 +57,8 @@ SOURCES += main.cpp\ sendtextedit.cpp \ feiqwin.cpp \ plugin/unreadchecker.cpp \ - plugin/iplugin.cpp + plugin/iplugin.cpp \ + plugin/rankuser.cpp HEADERS += mainwindow.h \ @@ -94,7 +95,8 @@ HEADERS += mainwindow.h \ sendtextedit.h \ plugin/iplugin.h \ feiqwin.h \ - plugin/unreadchecker.h + plugin/unreadchecker.h \ + plugin/rankuser.h FORMS += mainwindow.ui \ searchfellowdlg.ui \ diff --git a/feiqlib/uniqueid.cpp b/feiqlib/uniqueid.cpp index 44d3097..d8fdbac 100644 --- a/feiqlib/uniqueid.cpp +++ b/feiqlib/uniqueid.cpp @@ -9,7 +9,7 @@ UniqueId::UniqueId() IdType UniqueId::get() { auto id = ++mId; - if (id >= INT_MAX || id < 0) + if (id >= ULONG_LONG_MAX) mId=1; return mId; diff --git a/feiqwin.cpp b/feiqwin.cpp index e14ecfb..3cab028 100644 --- a/feiqwin.cpp +++ b/feiqwin.cpp @@ -46,7 +46,10 @@ void FeiqWin::init(MainWindow *mainWin) loadPlugins(); for (auto plugin : mPlugins) - plugin->init(this); + { + plugin->setFeiqWin(this); + plugin->init(); + } } void FeiqWin::unInit() diff --git a/fellowlistwidget.cpp b/fellowlistwidget.cpp index 75870d8..ba38a3d 100644 --- a/fellowlistwidget.cpp +++ b/fellowlistwidget.cpp @@ -17,8 +17,9 @@ void FellowListWidget::update(const Fellow &fellow) auto item = findFirstItem(fellow); if (item == nullptr) { - mWidget->addItem(fellowText(fellow)); - item = mWidget->item(mWidget->count()-1); + int row = requestRow(fellow); + mWidget->insertItem(row, fellowText(fellow)); + item = mWidget->item(row); } else { @@ -72,12 +73,17 @@ void FellowListWidget::mark(const Fellow &fellow, const QString &info) } } +void FellowListWidget::setRankPredict(FellowListWidget::RankPredict predict) +{ + mRankPredict = predict; +} + void FellowListWidget::itemChosen(QListWidgetItem *item) { if (item == nullptr) return; - auto fellow = static_cast(item->data(Qt::UserRole).value()); + auto fellow = getFellow(item); emit select(fellow); } @@ -97,10 +103,34 @@ QListWidgetItem *FellowListWidget::findFirstItem(const Fellow &fellow) for (int i = 0; i < count; i++) { auto widget = mWidget->item(i); - auto f = static_cast(widget->data(Qt::UserRole).value()); + auto f = getFellow(widget); if (f->getIp() == fellow.getIp()) return widget; } return nullptr; } + +int FellowListWidget::requestRow(const Fellow &fellow) +{ + auto count = mWidget->count(); + + if (!mRankPredict) + return count; + + int row = count; + for (; row > 0; row--) + { + auto f = getFellow(mWidget->item(row-1)); + auto ret = mRankPredict(*f, fellow); + if (ret >= 0) + break; + } + + return row; +} + +const Fellow *FellowListWidget::getFellow(const QListWidgetItem *item) +{ + return static_cast(item->data(Qt::UserRole).value()); +} diff --git a/fellowlistwidget.h b/fellowlistwidget.h index f53fd1d..971fae7 100644 --- a/fellowlistwidget.h +++ b/fellowlistwidget.h @@ -11,6 +11,8 @@ class FellowListWidget : public QObject Q_OBJECT public: + typedef std::function RankPredict; + FellowListWidget(); void bindTo(QListWidget* widget); @@ -19,6 +21,7 @@ public: void top(const Fellow& fellow); void topSecond(const Fellow& fellow); void mark(const Fellow& fellow, const QString &info); + void setRankPredict(RankPredict predict); signals: void select(const Fellow* fellow); @@ -29,8 +32,11 @@ private slots: private: QString fellowText(const Fellow& fellow); QListWidgetItem* findFirstItem(const Fellow& fellow); + int requestRow(const Fellow& fellow); + const Fellow* getFellow(const QListWidgetItem* item); private: + RankPredict mRankPredict; QListWidget* mWidget; }; diff --git a/mainwindow.cpp b/mainwindow.cpp index 7e50a90..6bed4f4 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -170,7 +170,7 @@ void MainWindow::handleFeiqViewEvent(shared_ptr event) } else if (event->what == ViewEventType::SEND_TIMEO || event->what == ViewEventType::MESSAGE) { - //地球人都知道这个分支中的ViewEvent集成自FellowViewEvent + //地球人都知道这个分支中的ViewEvent继承自FellowViewEvent auto e = static_cast(event.get()); auto fellow = e->fellow.get(); diff --git a/plugin/iplugin.cpp b/plugin/iplugin.cpp index c5c9e49..df0fe03 100644 --- a/plugin/iplugin.cpp +++ b/plugin/iplugin.cpp @@ -5,11 +5,20 @@ IPlugin::~IPlugin() } -void IPlugin::init(FeiqWin *feiqWin) +void IPlugin::setFeiqWin(FeiqWin *feiqWin) { mFeiq = feiqWin; } +void IPlugin::init() +{ +} + +void IPlugin::unInit() +{ + +} + PluginManager::PluginManager() { diff --git a/plugin/iplugin.h b/plugin/iplugin.h index ce06a71..f177b3d 100644 --- a/plugin/iplugin.h +++ b/plugin/iplugin.h @@ -9,9 +9,9 @@ class IPlugin { public: virtual ~IPlugin(); - virtual void init(FeiqWin* feiqWin); - - virtual void unInit() = 0; + void setFeiqWin(FeiqWin* feiqWin); + virtual void init(); + virtual void unInit(); protected: FeiqWin* mFeiq; diff --git a/plugin/rankuser.cpp b/plugin/rankuser.cpp new file mode 100644 index 0000000..e0715c7 --- /dev/null +++ b/plugin/rankuser.cpp @@ -0,0 +1,47 @@ +#include "rankuser.h" + +#define PLUGIN_NAME "rank_user" +REGISTER_PLUGIN(PLUGIN_NAME, RankUser) + +RankUser::RankUser() +{ +} + +void RankUser::init() +{ + IPlugin::init(); + connect(mFeiq->fellowListWidget(), SIGNAL(select(const Fellow*)), this, SLOT(onTalkTo(const Fellow*))); + mFeiq->fellowListWidget()->setRankPredict(std::bind(&RankUser::compare, this, placeholders::_1, placeholders::_2)); +} + +void RankUser::unInit() +{ + IPlugin::unInit(); + mFeiq->settings()->sync(); +} + +void RankUser::onTalkTo(const Fellow *fellow) +{ + mFeiq->settings()->setValue(fellowKey(*fellow), weightOfFellow(*fellow)+1); +} + +int RankUser::compare(const Fellow &f1, const Fellow &f2) +{ + return weightOfFellow(f1)-weightOfFellow(f2); +} + +QString RankUser::fellowId(const Fellow &f) +{ + QString ip(f.getIp().c_str()); + return ip; +} + +QString RankUser::fellowKey(const Fellow &f) +{ + return QString(PLUGIN_NAME)+"/"+fellowId(f); +} + +int RankUser::weightOfFellow(const Fellow &f) +{ + return mFeiq->settings()->value(fellowKey(f), "0").toInt(); +} diff --git a/plugin/rankuser.h b/plugin/rankuser.h new file mode 100644 index 0000000..d9d05c6 --- /dev/null +++ b/plugin/rankuser.h @@ -0,0 +1,28 @@ +#ifndef RANKUSER_H +#define RANKUSER_H + +#include "iplugin.h" +#include +#include "feiqlib/fellow.h" + +class RankUser : public QObject, public IPlugin +{ + Q_OBJECT + +public: + RankUser(); + + void init() override; + void unInit() override; + +private slots: + void onTalkTo(const Fellow* fellow); + +private: + int compare(const Fellow& f1, const Fellow& f2); + QString fellowId(const Fellow& f); + QString fellowKey(const Fellow& f); + int weightOfFellow(const Fellow& f); +}; + +#endif // RANKUSER_H diff --git a/plugin/unreadchecker.cpp b/plugin/unreadchecker.cpp index be16e5d..f64aa96 100644 --- a/plugin/unreadchecker.cpp +++ b/plugin/unreadchecker.cpp @@ -19,9 +19,9 @@ void UnreadChecker::timerEvent(QTimerEvent *event) } } -void UnreadChecker::init(FeiqWin *feiqWin) +void UnreadChecker::init() { - IPlugin::init(feiqWin); + IPlugin::init(); auto settings = mFeiq->settings(); mUnreadTimerInterval = settings->value(PLUGIN_NAME"/timer", "0").toInt(); diff --git a/plugin/unreadchecker.h b/plugin/unreadchecker.h index 31c868e..5820ed2 100644 --- a/plugin/unreadchecker.h +++ b/plugin/unreadchecker.h @@ -10,7 +10,7 @@ class UnreadChecker : public QObject, public IPlugin public: UnreadChecker(); - void init(FeiqWin* feiqWin) override; + void init() override; void unInit() override; protected: