Doxygen
tooltip.cpp
浏览该文件的文档.
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2020 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #include <map>
17 #include <memory>
18 #include <unordered_map>
19 #include <string>
20 #include <mutex>
21 
22 #include "tooltip.h"
23 #include "definition.h"
24 #include "outputgen.h"
25 #include "util.h"
26 #include "filedef.h"
27 #include "doxygen.h"
28 #include "config.h"
29 
30 static std::mutex g_tooltipsMutex;
31 static std::unordered_map<int, std::set<std::string> > g_tooltipsWrittenPerFile;
32 
34 {
35  public:
36  std::map<std::string,const Definition*> tooltipInfo;
37 };
38 
39 TooltipManager::TooltipManager() : p(std::make_unique<Private>())
40 {
41 }
42 
44 {
45 }
46 
47 static QCString escapeId(const QCString &s)
48 {
49  QCString res=s;
50  for (uint i=0;i<res.length();i++) if (!isId(res[i])) res[i]='_';
51  return res;
52 }
53 
55 {
56  bool sourceTooltips = Config_getBool(SOURCE_TOOLTIPS);
57  if (!sourceTooltips) return;
58 
59  QCString id = d->getOutputFileBase();
60  int i=id.findRev('/');
61  if (i!=-1)
62  {
63  id = id.right(id.length()-i-1); // strip path (for CREATE_SUBDIRS=YES)
64  }
65  // In case an extension is present translate this extension to something understood by the tooltip handler
66  // otherwise extend t with a translated htmlFileExtension.
67  QCString currentExtension = getFileNameExtension(id);
68  if (currentExtension.isEmpty())
69  {
71  }
72  else
73  {
74  id = stripExtensionGeneral(id,currentExtension) + escapeId(currentExtension);
75  }
76 
77  QCString anc = d->anchor();
78  if (!anc.isEmpty())
79  {
80  id+="_"+anc;
81  }
82  id = "a" + id;
83  p->tooltipInfo.insert(std::make_pair(id.str(),d));
84  //printf("%p: addTooltip(%s) ol=%d\n",this,id.data(),ol.id());
85 }
86 
88 {
89  int id = ol.id();
90  std::unordered_map<int, std::set<std::string> >::iterator it;
91  // critical section
92  {
93  std::lock_guard<std::mutex> lock(g_tooltipsMutex);
94  it = g_tooltipsWrittenPerFile.find(id);
95  if (it==g_tooltipsWrittenPerFile.end()) // new file
96  {
97  it = g_tooltipsWrittenPerFile.insert(std::make_pair(id,std::set<std::string>())).first;
98  }
99  }
100 
101  for (const auto &kv : p->tooltipInfo)
102  {
103  bool written = it->second.find(kv.first)!=it->second.end();
104  if (!written) // only write tooltips once
105  {
106  //printf("%p: writeTooltips(%s) ol=%d\n",this,kv.first.c_str(),ol.id());
107  const Definition *d = kv.second;
108  DocLinkInfo docInfo;
109  docInfo.name = d->qualifiedName();
110  docInfo.ref = d->getReference();
111  docInfo.url = d->getOutputFileBase();
112  docInfo.anchor = d->anchor();
113  SourceLinkInfo defInfo;
114  if (d->getBodyDef() && d->getStartBodyLine()!=-1)
115  {
116  defInfo.file = d->getBodyDef()->name();
117  defInfo.line = d->getStartBodyLine();
118  defInfo.url = d->getSourceFileBase();
119  defInfo.anchor = d->getSourceAnchor();
120  }
121  SourceLinkInfo declInfo; // TODO: fill in...
122  QCString decl;
124  {
125  const MemberDef *md = toMemberDef(d);
126  if (!md->isAnonymous())
127  {
128  decl = md->declaration();
129  }
130  }
131  ol.writeTooltip(kv.first.c_str(), // id
132  docInfo, // symName
133  decl, // decl
134  d->briefDescriptionAsTooltip(), // desc
135  defInfo,
136  declInfo
137  );
138  it->second.insert(kv.first); // remember we wrote this tooltip for the given file id
139  }
140  }
141 }
142 
Definition::getSourceFileBase
virtual QCString getSourceFileBase() const =0
stripExtensionGeneral
QCString stripExtensionGeneral(const QCString &fName, const QCString &ext)
Definition: util.cpp:5285
Definition::TypeMember
@ TypeMember
Definition: definition.h:90
g_tooltipsWrittenPerFile
static std::unordered_map< int, std::set< std::string > > g_tooltipsWrittenPerFile
Definition: tooltip.cpp:31
Definition
The common base class of all entity definitions found in the sources.
Definition: definition.h:76
QCString::length
uint length() const
Returns the length of the string, not counting the 0-terminator.
Definition: qcstring.h:147
QCString::findRev
int findRev(char c, int index=-1, bool cs=TRUE) const
Definition: qcstring.cpp:86
QCString::isEmpty
bool isEmpty() const
Returns TRUE iff the string is empty
Definition: qcstring.h:144
toMemberDef
MemberDef * toMemberDef(Definition *d)
Definition: memberdef.cpp:6088
TooltipManager::~TooltipManager
~TooltipManager()
Definition: tooltip.cpp:43
TooltipManager::p
std::unique_ptr< Private > p
Definition: tooltip.h:37
TooltipManager::TooltipManager
TooltipManager()
Definition: tooltip.cpp:39
uint
unsigned uint
Definition: qcstring.h:40
Definition::getBodyDef
virtual const FileDef * getBodyDef() const =0
Definition::qualifiedName
virtual QCString qualifiedName() const =0
outputgen.h
TooltipManager::Private::tooltipInfo
std::map< std::string, const Definition * > tooltipInfo
Definition: tooltip.cpp:36
MemberDef
A model of a class/file/namespace member symbol.
Definition: memberdef.h:45
isId
bool isId(int c)
Definition: util.h:172
g_tooltipsMutex
static std::mutex g_tooltipsMutex
Definition: tooltip.cpp:30
FileDef::name
virtual QCString name() const =0
Definition::isAnonymous
virtual bool isAnonymous() const =0
CodeOutputInterface::id
virtual int id() const
Identifier for the output file
Definition: outputgen.h:70
doxygen.h
tooltip.h
Definition::getOutputFileBase
virtual QCString getOutputFileBase() const =0
Definition::getStartBodyLine
virtual int getStartBodyLine() const =0
filedef.h
Definition::definitionType
virtual DefType definitionType() const =0
MemberDef::declaration
virtual QCString declaration() const =0
Definition::getReference
virtual QCString getReference() const =0
Definition::anchor
virtual QCString anchor() const =0
Config_getBool
#define Config_getBool(name)
Definition: config.h:33
Doxygen::htmlFileExtension
static QCString htmlFileExtension
Definition: doxygen.h:103
definition.h
CodeOutputInterface
Output interface for code parser.
Definition: outputgen.h:61
TooltipManager::addTooltip
void addTooltip(CodeOutputInterface &ol, const Definition *d)
add a tooltip for a given symbol definition
Definition: tooltip.cpp:54
CodeOutputInterface::writeTooltip
virtual void writeTooltip(const QCString &id, const DocLinkInfo &docInfo, const QCString &decl, const QCString &desc, const SourceLinkInfo &defInfo, const SourceLinkInfo &declInfo)=0
config.h
getFileNameExtension
QCString getFileNameExtension(const QCString &fn)
Definition: util.cpp:5621
Definition::briefDescriptionAsTooltip
virtual QCString briefDescriptionAsTooltip() const =0
escapeId
static QCString escapeId(const QCString &s)
Definition: tooltip.cpp:47
TooltipManager::writeTooltips
void writeTooltips(CodeOutputInterface &ol)
write the list of all collected tooltip to the given outputs
Definition: tooltip.cpp:87
util.h
A bunch of utility functions.
Definition::getSourceAnchor
virtual QCString getSourceAnchor() const =0
TooltipManager::Private
Definition: tooltip.cpp:33
QCString
This is an alternative implementation of QCString.
Definition: qcstring.h:108