2021-12-26 23:18:28 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2021-09-22 17:43:21 +08:00
|
|
|
#include "functiontooltipwidget.h"
|
|
|
|
|
|
|
|
#include <QHBoxLayout>
|
2021-09-25 21:34:10 +08:00
|
|
|
#include <QPushButton>
|
2021-09-22 17:43:21 +08:00
|
|
|
|
2021-09-25 21:34:10 +08:00
|
|
|
FunctionTooltipWidget::FunctionTooltipWidget(QWidget *parent) :
|
|
|
|
QFrame(parent, Qt::ToolTip | Qt::WindowStaysOnTopHint | Qt::WindowDoesNotAcceptFocus)
|
2021-09-22 17:43:21 +08:00
|
|
|
{
|
|
|
|
setFocusPolicy(Qt::NoFocus);
|
|
|
|
mInfoLabel = new QLabel(this);
|
|
|
|
mInfoLabel->setWordWrap(true);
|
|
|
|
mInfoLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Fixed);
|
2024-03-08 08:37:21 +08:00
|
|
|
mInfoLabel->setTextFormat(Qt::TextFormat::RichText);
|
2021-09-22 17:43:21 +08:00
|
|
|
mTotalLabel = new QLabel(this);
|
|
|
|
mTotalLabel->setWordWrap(false);
|
|
|
|
mTotalLabel->setSizePolicy(QSizePolicy::Fixed,QSizePolicy::Preferred);
|
|
|
|
mUpButton = new QToolButton(this);
|
|
|
|
mUpButton->setArrowType(Qt::UpArrow);
|
|
|
|
mUpButton->setFixedSize(16, 16);
|
|
|
|
mUpButton->setAutoRaise(true);
|
|
|
|
mDownButton = new QToolButton(this);
|
|
|
|
mDownButton->setArrowType(Qt::DownArrow);
|
|
|
|
mDownButton->setFixedSize(16, 16);
|
|
|
|
mDownButton->setAutoRaise(true);
|
2022-10-21 09:56:14 +08:00
|
|
|
mInfoIndex=0;
|
2021-09-22 17:43:21 +08:00
|
|
|
|
|
|
|
this->setLayout(new QHBoxLayout());
|
2021-09-25 21:34:10 +08:00
|
|
|
layout()->setContentsMargins(0,0,0,0);
|
|
|
|
layout()->setSpacing(0);
|
2021-09-22 17:43:21 +08:00
|
|
|
layout()->addWidget(mUpButton);
|
|
|
|
layout()->addWidget(mTotalLabel);
|
|
|
|
layout()->addWidget(mDownButton);
|
|
|
|
layout()->addWidget(mInfoLabel);
|
2021-09-25 21:34:10 +08:00
|
|
|
connect(mUpButton,&QPushButton::clicked,
|
|
|
|
this,&FunctionTooltipWidget::previousTip);
|
|
|
|
connect(mDownButton,&QPushButton::clicked,
|
|
|
|
this,&FunctionTooltipWidget::nextTip);
|
2021-09-22 17:43:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::addTip(const QString &name, const QString& fullname,
|
|
|
|
const QString &returnType, const QString &args, const QString &noNameArgs)
|
|
|
|
{
|
|
|
|
PFunctionInfo info = std::make_shared<FunctionInfo>();
|
|
|
|
info->name = name;
|
|
|
|
info->fullname = fullname;
|
|
|
|
info->returnType = returnType;
|
|
|
|
info->params = splitArgs(args);
|
|
|
|
info->nonameParams = splitArgs(noNameArgs);
|
2021-09-24 11:41:14 +08:00
|
|
|
mInfos.append(info);
|
2021-09-22 17:43:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::clearTips()
|
|
|
|
{
|
2022-10-21 09:56:14 +08:00
|
|
|
mInfoIndex=0;
|
2021-09-22 17:43:21 +08:00
|
|
|
mInfos.clear();
|
|
|
|
hide();
|
|
|
|
}
|
|
|
|
|
2021-09-24 22:49:36 +08:00
|
|
|
int FunctionTooltipWidget::tipCount()
|
|
|
|
{
|
|
|
|
return mInfos.count();
|
|
|
|
}
|
|
|
|
|
2021-09-22 17:43:21 +08:00
|
|
|
int FunctionTooltipWidget::paramPos() const
|
|
|
|
{
|
|
|
|
return mParamPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::setParamPos(int newParamPos)
|
|
|
|
{
|
|
|
|
mParamPos = newParamPos;
|
|
|
|
}
|
|
|
|
|
2021-09-24 11:41:14 +08:00
|
|
|
void FunctionTooltipWidget::nextTip()
|
2021-09-22 17:43:21 +08:00
|
|
|
{
|
2021-10-04 21:59:48 +08:00
|
|
|
if (mInfoIndex>=mInfos.length()-1) {
|
|
|
|
hide();
|
|
|
|
return;
|
|
|
|
}
|
2021-09-24 11:41:14 +08:00
|
|
|
if (mInfos.length()>0)
|
|
|
|
mInfoIndex = std::min(mInfoIndex+1,mInfos.length()-1);
|
|
|
|
updateTip();
|
2021-09-22 17:43:21 +08:00
|
|
|
}
|
|
|
|
|
2021-09-24 11:41:14 +08:00
|
|
|
void FunctionTooltipWidget::previousTip()
|
2021-09-22 17:43:21 +08:00
|
|
|
{
|
2021-10-04 21:59:48 +08:00
|
|
|
if (mInfoIndex==0) {
|
|
|
|
hide();
|
|
|
|
return ;
|
|
|
|
}
|
2021-09-24 11:41:14 +08:00
|
|
|
if (mInfos.length()>0)
|
|
|
|
mInfoIndex = std::max(mInfoIndex-1,0);
|
|
|
|
updateTip();
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::updateTip()
|
|
|
|
{
|
|
|
|
mTotalLabel->setVisible(mInfos.length()>1);
|
|
|
|
mUpButton->setVisible(mInfos.length()>1);
|
|
|
|
mDownButton->setVisible(mInfos.length()>1);
|
|
|
|
mUpButton->setEnabled(mInfoIndex!=0);
|
|
|
|
mDownButton->setEnabled(mInfoIndex!=mInfos.length()-1);
|
|
|
|
if (mInfos.length()<=0)
|
|
|
|
return;
|
|
|
|
PFunctionInfo info = mInfos[mInfoIndex];
|
|
|
|
QString text = info->returnType+ " " + info->name;
|
|
|
|
if (info->params.length()==0) {
|
|
|
|
text += "()";
|
|
|
|
} else {
|
|
|
|
QStringList displayList;
|
|
|
|
for (int i=0;i<info->params.length();i++){
|
|
|
|
const QString& param = info->params[i];
|
|
|
|
if (mParamIndex == i) {
|
|
|
|
displayList.append(QString("<b>%1</b>").arg(param));
|
|
|
|
} else {
|
|
|
|
displayList.append(param);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
text += "( "+displayList.join(", ") + ") ";
|
|
|
|
}
|
2021-09-25 21:34:10 +08:00
|
|
|
if (mInfos.length()>1) {
|
|
|
|
mTotalLabel->setText(QString("%1/%2").arg(mInfoIndex+1).arg(mInfos.length()));
|
|
|
|
}
|
|
|
|
int width = mInfoLabel->fontMetrics().horizontalAdvance(text);
|
|
|
|
if (width > 400) {
|
|
|
|
mInfoLabel->setMinimumWidth(410);
|
|
|
|
} else {
|
|
|
|
mInfoLabel->setMinimumWidth(width);
|
|
|
|
}
|
2021-09-24 11:41:14 +08:00
|
|
|
mInfoLabel->setText(text);
|
2021-09-22 17:43:21 +08:00
|
|
|
}
|
|
|
|
|
2021-09-25 08:41:11 +08:00
|
|
|
void FunctionTooltipWidget::guessFunction(int commas)
|
2021-09-25 08:40:18 +08:00
|
|
|
{
|
2021-09-25 21:34:10 +08:00
|
|
|
if (mInfoIndex>=0 && mInfoIndex<mInfos.count()
|
|
|
|
&& mInfos[mInfoIndex]->params.count()>commas)
|
|
|
|
return;
|
2021-09-25 08:40:18 +08:00
|
|
|
for (int i=0;i<mInfos.size();i++) {
|
|
|
|
if (mInfos[i]->params.count()>commas) {
|
|
|
|
mInfoIndex = i;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mInfoIndex = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-09-23 12:06:26 +08:00
|
|
|
QStringList FunctionTooltipWidget::splitArgs(QString argStr)
|
2021-09-22 17:43:21 +08:00
|
|
|
{
|
2021-09-23 12:06:26 +08:00
|
|
|
int i = 0;
|
2021-09-22 17:43:21 +08:00
|
|
|
// Split up argument string by ,
|
|
|
|
if (argStr.startsWith('(')) {
|
|
|
|
i = 1; // assume it starts with ( and ends with )
|
|
|
|
} else {
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
int paramStart = i;
|
|
|
|
|
|
|
|
QStringList result;
|
|
|
|
while (i < argStr.length()) {
|
2021-09-23 12:06:26 +08:00
|
|
|
if ((argStr[i] == ',')) {
|
2021-09-22 17:43:21 +08:00
|
|
|
// We've found "int* a" for example
|
|
|
|
QString s = argStr.mid(paramStart,i-paramStart);
|
2021-09-23 12:06:26 +08:00
|
|
|
result.append(s);
|
2021-09-22 17:43:21 +08:00
|
|
|
paramStart = i + 1; // step over ,
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2021-09-23 12:06:26 +08:00
|
|
|
QString s = argStr.mid(paramStart,i-paramStart);
|
|
|
|
s=s.trimmed();
|
|
|
|
if (!s.isEmpty()) {
|
2021-09-25 21:34:10 +08:00
|
|
|
if (s.endsWith(')'))
|
|
|
|
s.truncate(s.length()-1);
|
2021-09-23 12:06:26 +08:00
|
|
|
result.append(s);
|
|
|
|
}
|
2021-09-22 17:43:21 +08:00
|
|
|
return result;
|
|
|
|
}
|
2021-09-24 11:41:14 +08:00
|
|
|
|
|
|
|
const QString &FunctionTooltipWidget::functionFullName() const
|
|
|
|
{
|
|
|
|
return mFunctioFullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::setFunctioFullName(const QString &newFunctioFullName)
|
|
|
|
{
|
|
|
|
mFunctioFullName = newFunctioFullName;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FunctionTooltipWidget::paramIndex() const
|
|
|
|
{
|
|
|
|
return mParamIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::setParamIndex(int newParamIndex)
|
|
|
|
{
|
|
|
|
mParamIndex = newParamIndex;
|
2021-09-25 21:34:10 +08:00
|
|
|
updateTip();
|
2021-09-24 11:41:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::closeEvent(QCloseEvent *)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionTooltipWidget::showEvent(QShowEvent *)
|
|
|
|
{
|
2021-09-25 08:40:18 +08:00
|
|
|
if (mInfoIndex<0 || mInfoIndex>= mInfos.count()) {
|
2021-09-24 11:41:14 +08:00
|
|
|
mInfoIndex = 0;
|
|
|
|
}
|
|
|
|
updateTip();
|
|
|
|
}
|
|
|
|
|
2021-09-25 21:34:10 +08:00
|
|
|
void FunctionTooltipWidget::hideEvent(QHideEvent *)
|
2021-09-24 11:41:14 +08:00
|
|
|
{
|
|
|
|
mInfos.clear();
|
2021-09-25 21:34:10 +08:00
|
|
|
mFunctioFullName = "";
|
2021-09-24 11:41:14 +08:00
|
|
|
}
|